Multi-level JSON

Hi. I didn't get it how to use multiple-level json in KM. I can access 1st level items, but not deeper. Example:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"jhhKHJJjjHJHKJk/JKJLjlkkklkkJ-EPT1GU\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"JLKJLKJkjkljkJKLJLKJKJLJL/KLJLKJLKJKjkJKJKJJKJLJ\"",
   "id": "KJJLJLKJKJKKLJLJ_LKJKJL",
   "statistics": {
    "viewCount": "41",
    "commentCount": "0",
    "subscriberCount": "3",
    "hiddenSubscriberCount": false,
    "videoCount": "2"
   }
  }
 ]
}

How can I get "subscriberCount" value? Thank you

With a variable called JSON set to that text, you can use the JSON path:

JSON.items[1].statistics.subscriberCount

to get the subscriberCount of the first item.

That is, the JSON variable contains a dictionary, and we look at the items array, the first element, which is a dictionary that includes the statistics entry, which itself is a dictionary that includes the subscriberCount.

Note that you can use JSON.items[0] to get the number of items if you want to iterate through all of them.

So, something like this:

image

Or to list all of them, any of these would work:

image

The first one using JSONVALUE(JSON.items[0]) to get the number of entries and iterates through them from 1 up to that.

The second one gets the keys indexing the JSONVALUE(JSON.items) array (which are just numbers from 1 up to the count of items), and iterates through them using that key.

And the last one iterates explicitly through the items, returning a JSON object {"key":KEY,"value":VALUE} for each items, where KEY is the index, and VALUE is the value, hence you can see JSONV.value.statistics.subscriberCount in that last one, that is where the value comes from.

9 Likes

Thank you very much, very detailed, clear and prompt answer.

Great example, Peter. We need to get something like that in the KM Wiki.
Maybe we need a Wiki page dedicated to JSON, like the RegEx page.

3 Likes

Yes, I expect so. There is the JSON user manual page, but that needs to remain as part of the manual (so more reference). But we definitely will need to do more along the likes of the RegEx page as well.