W.Lyman
W.Lyman

Reputation: 347

For loop json Google Script

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}

Upvotes: 1

Views: 169

Answers (3)

W.Lyman
W.Lyman

Reputation: 347

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);```

Upvotes: 2

Giampaolo Ferradini
Giampaolo Ferradini

Reputation: 587

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?

Upvotes: 1

andrewJames
andrewJames

Reputation: 22012

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.

Upvotes: 1

Related Questions