# Extracting values from JSON response

**URL:** <https://community.postman.com/t/extracting-values-from-json-response/20520>\
**Category:** Help Hub\
**Created:** [February 18, 2021, 6:50am UTC](https://community.postman.com/t/extracting-values-from-json-response/20520 "2021-02-18T06:50:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okuae](https://avatars.discourse-cdn.com/v4/letter/o/c67d28/32.png) [@okuae](https://community.postman.com/u/okuae)\
**Post date:** [February 18, 2021, 6:50am UTC](https://community.postman.com/t/extracting-values-from-json-response/20520/1 "2021-02-18T06:50:41Z")

</div>

Hey,

I have been trying to store information to a variable based on all the same Names visible on the page with the following JSON code:

```auto
[
    { 
        "Name": "James", 
        "Id": 9999,
        "Values": [
            {
            "work": "army",
            "location": "USA"
            }
         ]

    }, 
    {
        "Name": "Jessica", 
        "Id": 34332
    }
]

```

This is the code I have tried to do but it only gives errors:

```auto
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?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [February 18, 2021, 12:59pm UTC](https://community.postman.com/t/extracting-values-from-json-response/20520/2 "2021-02-18T12:59:25Z")

</div>

Hey @okuae

It’s not far off, just need to change something:

```auto
_.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.

```auto
let names = []
_.each(jsonData, (result) => {
    names.push(result.Name)
})
pm.globals.set('Names of the people', JSON.stringify(names))

```

You could also use [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) if you wanted to reduce the script down some more:

```auto
const names = jsonData.map(o => o.Name)

pm.globals.set('Names of the people', JSON.stringify(names))

```
