AnsweredAssumed Answered

JSONListKeys Assistance

Question asked by fmdataweb on Sep 4, 2018
Latest reply on Sep 5, 2018 by beverly


I'm trying to parse the following JSON that I receive from an API:

 

{

  "Ack": "Success",

  "CurrentTime": "2018-09-04 15:17:14",

  "Item": [

    {

      "BuyUnitQuantity": "1",

      "Categories": [

        {

          "Category": {

            "CategoryID": "99",

            "CategoryName": "[Sample] Sub Category",

            "Priority": "0"

          }

        }

      ],

      "CommittedQuantity": "6"

    },

    {

      "BuyUnitQuantity": "1",

      "Categories": [

        {

          "Category": {

            "CategoryID": "99",

            "CategoryName": "[Sample] Sub Category",

            "Priority": "0"

          }

        }

      ],

      "CommittedQuantity": "0"

    },

    {

      "BuyUnitQuantity": "1",

      "Categories": [

        {

          "Category": [

            {

              "CategoryID": "99",

              "CategoryName": "[Sample] Sub Category",

              "Priority": "0"

            },

            {

              "CategoryID": "101",

              "CategoryName": "[Sample] Sub Category 2",

              "Priority": "0"

            }

          ]

        }

      ],

      "CommittedQuantity": "0"

    }

  ]

}

 

I'm getting the following results for the count of the number of Categories returned for each Item:

 

ValueCount ( JSONListKeys ( $jsonData ; "Item[0].Categories" ) ) = 1

ValueCount ( JSONListKeys ( $jsonData ; "Item[1].Categories" ) ) = 1

ValueCount ( JSONListKeys ( $jsonData ; "Item[2].Categories" ) ) = 1

 

I can see that in the last Item there are 2 Categories, but

 

ValueCount ( JSONListKeys ( $jsonData ; "Item[2].Categories" ) )

 

is only returning 1. It looks like when there is more than 1 Category it's creating another array (?) but I'm not sure how to dynamically handle this in my script that loops through each Item to pull out the data. Any ideas on how I can get all the Categories regardless of if there is only 1 or more than 1 in each Item?

Outcomes