Xbasic

json_flatten_default Function

Syntax

C jsonOut = json_flatten_default(c jsonIn)

Arguments

jsonInCharacter

The JSON containing nested arrays.

Returns

jsonOutCharacter

Returns the JSON flattened with no nested arrays.

Description

Takes JSON with nested arrays and "flattens" the JSON so that there are no nested arrays.

Discussion

The json_flatten_default() function converts a JSON string with nested arrays into a JSON string with no nested arrays. This is same as the json_flatten() function, but the template required by the json_flatten() function is automatically generated from the input JSON.

In the example below, notice that each parent record is repeated for each record in the nested array.

Example

dim json as c 
json = <<%str%
[
    {"name": "John", "Lastname" : "Smith", "City" : "Boston", "State" : "MA", "Children": [
            {"Name" : "Callie", "Age" : 5},
            {"Name" : "Griffin", "Age" :3},
            {"Name" : "Luke", "Age" : 1}
        ]
    }, 
    {"name": "Henry", "Lastname" : "Rhodes", "City" : "New York", "State" : "NY", "Children": [
            {"Name" : "Howard", "Age" : 15},
            {"Name" : "Robert", "Age" : 11}
        ]
    }, 
    {"name": "Allison", "Lastname" : "Berman", "City" : "Los Angeles", "State" : "CA", "Children": [
            {"Name" : "Jeff", "Age" : 35},
            {"Name" : "Roxanne", "Age" :33},
            {"Name" : "Claudia", "Age" : 31},
            {"Name" : "Denzel", "Age" : 11}
        ]
    }
]
%str%
dim json2 as c
json2 = json_flatten_default(json)
?json2

= [
{"name": "John","Lastname": "Smith","City": "Boston","State": "MA","Children_Name": "Callie","Children_Age": 5},
{"name": "John","Lastname": "Smith","City": "Boston","State": "MA","Children_Name": "Griffin","Children_Age": 3},
{"name": "John","Lastname": "Smith","City": "Boston","State": "MA","Children_Name": "Luke","Children_Age": 1},
{"name": "Henry","Lastname": "Rhodes","City": "New York","State": "NY","Children_Name": "Howard","Children_Age": 15},
{"name": "Henry","Lastname": "Rhodes","City": "New York","State": "NY","Children_Name": "Robert","Children_Age": 11},
{"name": "Allison","Lastname": "Berman","City": "Los Angeles","State": "CA","Children_Name": "Jeff","Children_Age": 35},
{"name": "Allison","Lastname": "Berman","City": "Los Angeles","State": "CA","Children_Name": "Roxanne","Children_Age": 33},
{"name": "Allison","Lastname": "Berman","City": "Los Angeles","State": "CA","Children_Name": "Claudia","Children_Age": 31},
{"name": "Allison","Lastname": "Berman","City": "Los Angeles","State": "CA","Children_Name": "Denzel","Children_Age": 11}
]