Search code examples
jsonpowerapps

Turning nested JSON array into nested collection in PowerApps


I am having huge problems with turning nested arrays within JSON into nested collections. For example, I can't work out how to reverse the process below to turn the tempJSON string back into the startTable.

ClearCollect(
    startTable,
    Table(
        {
            OrderNo: 1,
            Person: "Tim",
            Details: Table(
                {
                    ItemCode: "X43",
                    Quantity: 5
                },
                {
                    ItemCode: "X44",
                    Quantity: 3
                }
            )
        },
        {
            OrderNo: 2,
            Person: "Tom",
            Details: Table(
                {
                    ItemCode: "X45",
                    Quantity: 1
                },
                {
                    ItemCode: "X46",
                    Quantity: 6
                }
            )
        }
    )
);

Set(tempJSON,JSON(startTable));

I just can't get my head around how to use ForAll functions to unpack it... e.g.

ForAll(
    Table(ParseJSON(tempJSON)),
    {
        OrderNo:Int(ThisRecord.Value.OrderNo),
        Person:Text(ThisRecord.Value.Person),
        Details:????????)
    }
)

Solution

  • You will need to use another call to ForAll for each of the records from the table, like in the example below:

    ClearCollect(
        endTable,
        ForAll(
            ParseJSON(tempJSON),
            {
                Person: Text(ThisRecord.Person),
                OrderNo: Value(ThisRecord.OrderNo),
                Details: ForAll(
                    ThisRecord.Details,
                    {
                        ItemCode: Text(ThisRecord.ItemCode),
                        Quantity: Value(ThisRecord.Quantity)
                    }
                )
            }
        )
    )
    

    This should give you your original table.

    Notice that the ForAll function can work directly with the output of ParseJSON, you don't need to call the Table function on it.