This is the code I have tried to do but it only gives errors:
let jsonData = pm.response.json()
_.each(jsonData.name, (result) => {
pm.globals.set('Names of the people', name)
})
Is there something I need to add? since the first JSON array/object is nameless, do I need to add something extra?
Would I also be able to use this somehow to list all values > locations?
It’s not far off, just need to change something:
_.each(jsonData, (result) => {
pm.globals.set('Names of the people', result.Name)
})
jsonData is the array that is going to be looped over, each object in the array would be captured in the result argument. To get values from the result you would need to use result.Name or any other property you have in the object.
As this has more than 1 object in the array, it will set the first objects name and the variable and then overwrite that with the second objects name.
If you wanted to grab each name and create an array fro them you can modify what you have and push each name it a new local variable before strong that as a global variable.
let names = []
_.each(jsonData, (result) => {
names.push(result.Name)
})
pm.globals.set('Names of the people', JSON.stringify(names))
You could also use map if you wanted to reduce the script down some more:
const names = jsonData.map(o => o.Name)
pm.globals.set('Names of the people', JSON.stringify(names))