Xbasic

Extension::JsonJSONShred Method

Syntax

.JSONShred as c (JSON as C, JSONFieldtemplate as C [, topName as C])

Arguments

JSONCharacter

JSON to 'shred'

JSONFieldtemplateCharacter

Shape of the JSON we want to 'shred'. In addition to fields specified by {<fieldname>}, there are built fields, described below.

{./begincurly}

Emit a '{' in the template.

{./endcurly}

Emit a '}' in the template.

{./number}

When included, Emit this field as a numeric value rather than string value.

{./logical}

When included, Emit this field as a logical value rather than string value.

{./replace}

When included, indicate that this field replaces an existing field (remove the field if it exists).

{./delete}

The field named is not emitted, but instead causes an existing field to be removed.

{./wrap}

Emits an object field that 'contains' all the fields that follow (including the existing fields).

{./row}

Emit counter for the 'row' in the current branch.

{./count}

Emit the number of rows in the current branch.

{./index}

Emit counter for the 'row' from that first branch to the current location.

{./start}

Emit the 'index' for the first row in the current branch.

{./rowz}

Emit counter for the 'row' in the current branch, but assume numbering starts a '0' instead of '1'.

{./indexz}

Emit counter for the 'row' from that first branch to the current location, but assume numbering starts a '0' instead of '1'.

{./startz}

Emit the 'index' for the first row in the current branch, but assume numbering starts a '0' instead of '1'.

{./<child>/./start}

Emit the 'start' Index of the first entry in a specified child array field.

{./<child>/./count}}

Emit the count of the entries in a specified child array field.

{./<child>/./startz}

Emit the 'start' Index of the first entry in a specified child array field, but assume numbering starts a '0' instead of '1'.

topNameCharacter

Optional 'topname' (if ommitted __top) is used. This is the name of the parent array in the result.

Description

Takes a complex JSON structure with nested arrays and turns it into a flat list of arrays with no nested arrays. The way in which the JSON should be 'shredded' is described by a template (which can contain definitions for 'key' fields to be added to the child arrays in the result).

Example Shred Parent/Child/Grandchild JSON

In this example, each levels already has all the keys required. Therfore, we we just need to describe the 'shape' of JSON data we want to shred. (The template does not need to define any 'key' fields.)

dim schema as c 
schema = <<%str%
[
    {
        "orders" : [
            {
                "OrderItems" : [
                  { 
                  }
                ]
            }
        ]
    }
]
%str%

dim JSON as c = <<%str%
[
        {
            "CustomerID": "ALFKI",
            "CompanyName": "Alfreds Futterkistes",
            "orders": [
                {
                    "OrderID": "10643",
                    "CustomerID": "ALFKI",
                    "OrderDate": "08/25/1997 12:00:00 000 am",
                    "orderItems": [
                        {
                            "OrderID": "10643",
                            "ProductID": "28",
                            "Quantity": "15",
                            "UnitPrice": "2"
                        },
                        {
                            "OrderID": "10643",
                            "ProductID": "39",
                            "Quantity": "21",
                            "UnitPrice": "18"
                        }
                    ]
                },
                {
                    "OrderID": "10702",
                    "CustomerID": "ALFKI",
                    "OrderDate": "10/13/1997 12:00:00 000 am",
                    "orderItems": [
                        {
                            "OrderID": "10702",
                            "ProductID": "3",
                            "Quantity": "6",
                            "UnitPrice": "10"
                        },
                        {
                            "OrderID": "10702",
                            "ProductID": "76",
                            "Quantity": "15",
                            "UnitPrice": "18"
                        }
                    ]
                }
            ]
        },
        {
            "CustomerID": "ANATR",
            "CompanyName": "Ana Trujillo Emparedados y helados",
            "orders": [
                {
                    "OrderID": "10308",
                    "CustomerID": "ANATR",
                    "OrderDate": "09/18/1996 12:00:00 000 am",
                    "orderItems": [
                        {
                            "OrderID": "10308",
                            "ProductID": "69",
                            "Quantity": "1",
                            "UnitPrice": "3"
                        },
                        {
                            "OrderID": "10308",
                            "ProductID": "70",
                            "Quantity": "5",
                            "UnitPrice": "12"
                        }
                    ]
                }
            ]
        }
]
%str%

dim out as c = extension::JSON::JSONShred(JSON,schema)

The output produced by the shredding operation is an object where the nested data is collected into arrays under a single top level object (called '__top' in this example because the optional topName parameter was not supplied).

{
    "__top": [
        {
            "CustomerID": "ALFKI",
            "CompanyName": "Alfreds Futterkistes"
        },
        {
            "CustomerID": "ANATR",
            "CompanyName": "Ana Trujillo Emparedados y helados"
        }
    ],
    "orders": [
        {
            "OrderID": "10643",
            "CustomerID": "ALFKI",
            "OrderDate": "08/25/1997 12:00:00 000 am"
        },
        {
            "OrderID": "10702",
            "CustomerID": "ALFKI",
            "OrderDate": "10/13/1997 12:00:00 000 am"
        },
        {
            "OrderID": "10308",
            "CustomerID": "ANATR",
            "OrderDate": "09/18/1996 12:00:00 000 am"
        }
    ],
    "OrderItems": [
        {
            "OrderID": "10643",
            "ProductID": "28",
            "Quantity": "15",
            "UnitPrice": "2"
        },
        {
            "OrderID": "10643",
            "ProductID": "39",
            "Quantity": "21",
            "UnitPrice": "18"
        },
        {
            "OrderID": "10702",
            "ProductID": "3",
            "Quantity": "6",
            "UnitPrice": "10"
        },
        {
            "OrderID": "10702",
            "ProductID": "76",
            "Quantity": "15",
            "UnitPrice": "18"
        },
        {
            "OrderID": "10308",
            "ProductID": "69",
            "Quantity": "1",
            "UnitPrice": "3"
        },
        {
            "OrderID": "10308",
            "ProductID": "70",
            "Quantity": "5",
            "UnitPrice": "12"
        }
    ]
}