1

I can't for the life of me figure out how to loop over the following json snippet to retrieve the resource_id value to produce the following list {1, 2, 3}.

var dataStringify = 
    {
    "items": [
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "1"
                        }
                    ]
                }
            ]
        },
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "2"
                        }
                    ]
                }
            ]
        },
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "3"
                        }
                    ]
                }
            ]
        }
    ]
}

I have gotten as far as logging the following to determine that iterating on items with the following dot notation would yield approximately what I need, but I can't seem to be able write a working loop.

Logger.log(dataStringify.items[0].events[0].parameters[1].value);

this yields just the first value "1" rather than a list {1,2,3}

W.Lyman
  • 357
  • 1
  • 9
  • I think u need to do parameters[0] instead of [1], dataStringify.items[0].events[0].parameters[0].value – Chemi Adel Sep 01 '21 at 23:57
  • Oh that was a mistake. I tried to simplify the JSON. In the original the 1 was needed to access the right parameter. – W.Lyman Sep 02 '21 at 00:17

3 Answers3

2

I ended up using a combination of the two answers above. The script below works remarkably well. Thanks everyone!

var resultsArray = [];

for(var i = 0; i<3; i++) {
resultsArray.push(dataStringify.items[i].events[0].parameters[1].value)
}

console.log(resultsArray);```
W.Lyman
  • 357
  • 1
  • 9
1

You state you want "a list" {1,2,3} - but that { ... } is an object and as such it has to contain key: value pairs. So your example output is not valid.

If you want an array [ 1, 2, 3] instead, then one approach is to drill down into the nested arrays as follows:

var dataStringify = 
    {
    "items": [
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "1"
                        }
                    ]
                }
            ]
        },
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "2"
                        }
                    ]
                }
            ]
        },
        {
            "events": [
                {
                    "parameters": [
                        {
                            "name": "resource_id",
                            "value": "3"
                        }
                    ]
                }
            ]
        }
    ]
};

var resultsArray = [];

dataStringify.items.forEach((item) => {
  item.events.forEach((event) => {
    event.parameters.forEach((parameter) => {
      resultsArray.push( parameter.value );
    } );
  } );
} );

console.log( resultsArray );

The result is an array of strings, since that is how the source data is arranged:

[ "1", "2", "3" ]

If you want [ 1, 2, 3 ] then you can convert the strings to integers as you push them to the results array.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • This is a great answer. Very thorough and well explained, but when I fit it back into my original json with a bunch of other 'items', 'events', and 'parameters' it didn't work correctly and just printed off all values for all parameters. Overall I feel like your answer is better but it didn't apply back to my original script as well. – W.Lyman Sep 02 '21 at 01:58
1

How about:

for(var i = 0; i<3; i++) {
  Logger.log(dataStringify.items[i].events[0].parameters[0].value);
}

items is the variable you want to loop around, right?

Giampaolo Ferradini
  • 529
  • 1
  • 6
  • 17