Xbasic

json_composite Function

Syntax

json_composite(parent as C, relations as C, jsonText as C [,mode as C [,limits as C [,views as C [,calcs as C]]]] )

Arguments

parentCharacter

The top-most node of the tree. IE, the 'root'.

relationsCharacter

JSON object defining the data relationships.

jsonTextCharacter

The flat JSON object.

modeCharacter

Can be 'sparse' to eliminate child keys from the output.

limitsCharacter

'table' : 'limit' pairs.

viewsCharacter

Defines groups to extract from the flattened tables.

calcsCharacter

Description

Turns flat lists into a tree structure.

Discussion

json_composite creates a tree from a flat JSON object using a reference example relational structure.

dim relations as c =<<%str%
{
    "orders" : { "customerId" : "id", "orderDetails" : { "orderId2" : "orderId" } }
}
%str% 
dim def as c
def =<<%txt%
{
    "customers": [
        { "id": "alfki", "name": "305 - 14th Ave. S.\nSuite 3B"},
        { "id": "bolid", "name":"name 2"}
    ],
    "orders": [
        { "orderId": 1, "customerId": "alfki", "orderData": "12/1/2013"},
        { "orderId": 2, "customerId": "bolid", "orderData": "12/2/2013"},
        { "orderId": 3, "customerId": "alfki", "orderData": "12/3/2013"},
        { "orderId": 4, "customerId": "alfki","orderData": "12/1/2013"}
    ], 
    "orderDetails": [
        { "lineitemId": 1, "orderId2": 1, "prodId": 1, "qty": 3},
        { "lineitemId": 2, "orderId2": 2, "prodId": 6, "qty": 6},
        { "lineitemId": 3, "orderId2": 2, "prodId": 9, "qty": 7},
        { "lineitemId": 4, "orderId2": 1, "prodId":3, "qty": 2},
        { "lineitemId": 5, "orderId2": 1, "prodId": 3, "qty": 8},
        { "lineitemId": 6, "orderId2": 3, "prodId": 4, "qty": 9},
        { "lineitemId": 7, "orderId2": 3, "prodId": 3, "qty": 1}, 
        { "lineitemId": 8, "orderId2": 6, "prodId": 2, "qty": 3}
    ]
}
%txt%

t1 = now()
delete output
output = json_composite("customers",relations,def,"")
t2 = now()

showvar( json_reformat("{"+output+"}",.t.), "took "+(t2-t1))

See Also