# Parsing JSON Request. Find object in array

**URL:** <https://community.postman.com/t/parsing-json-request-find-object-in-array/32275>\
**Category:** Help Hub\
**Created:** [February 6, 2022, 11:56am UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275 "2022-02-06T11:56:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikelMaesoMMM](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@MikelMaesoMMM](https://community.postman.com/u/MikelMaesoMMM)\
**Post date:** [February 6, 2022, 11:56am UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275/1 "2022-02-06T11:56:40Z")

</div>

_My question_:

I want to access a value in a certain array repetition.  
This is the JSON:

```auto
{
    "d": {
        "results": [{
                "Id": 1,
				"Value": "XX"
            }
        ],
        [{
                "Id": 2,
				"Value": "YY"
            }
        ]
    }
}

```

I would like to do something like this:

```auto
var jsonData = JSON.parse(responseBody);
console.log(jsonData.d.results[Id='1'].Value);

```

Is it possible?  
What’s the proper approach?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [February 6, 2022, 4:41pm UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275/2 "2022-02-06T16:41:59Z")

</div>

Here are a few examples of working with arrays…

```auto
const response = pm.response.json();
pm.test("Test", () => {
  pm.expect(response).to.have.property("id", 0);
  pm.expect(response.tags[0].id).to.eql(0);
  pm.expect(response.tags).to.have.lengthOf(1);
});

```

Here is an image to show the JSON layout, to compare to the assertions above.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/8/e81894cab6070b84aa80ad2b193f1d9e521d6a03.png)

For a consol log you could do;

`console.log(jsonData.d.results[0].id);`

Note that the [0] would be looking at the first item in the array… if you wanted the second it would be [1] third would be [2] and so on (remember array indexes start at 0).

You can read more [here](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/)

---

<div class="post-metadata">

**Author:** ![MikelMaesoMMM](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@MikelMaesoMMM](https://community.postman.com/u/MikelMaesoMMM)\
**Post date:** [February 6, 2022, 5:16pm UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275/3 "2022-02-06T17:16:28Z")

</div>

Sorry, I think I didn’t explain myself properly.  
Maybe my example was to simple to explain what I was looking for.

I don’t want to access the value based on the index of the array (jsonData.d.results[0].value)), but on another value of the array (jsonData.d.results[Id=‘1’].Value).

In XPATH this is achievable without iterating all the entries, using expressions like:  
Get all the book which genre is Romance:

```auto
//book[genres/genre = 'Romance']

```

Thanks anyway.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [February 6, 2022, 6:29pm UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275/4 "2022-02-06T18:29:11Z")

</div>

Maybe try something like;

```auto
const response = pm.response.json();
_.each(pm.response.json().book, (book) => {
    if(book.genre === "Romance"){
        console.log(book.genre)
    }
})

```

---

<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 7, 2022, 9:45am UTC](https://community.postman.com/t/parsing-json-request-find-object-in-array/32275/5 "2022-02-07T09:45:13Z")

</div>

You could also use [.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to get the object which has an `Id` of `1`

```auto
let obj = pm.response.json().d.results.find(e => e.Id === 1);
console.log(obj.Value);

```
