# How to handle dynamic jsonPath

**URL:** <https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949>\
**Category:** Help Hub\
**Created:** [June 17, 2019, 10:52pm UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949 "2019-06-17T22:52:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vn0gdpo](https://avatars.discourse-cdn.com/v4/letter/v/71e660/32.png) [@vn0gdpo](https://community.postman.com/u/vn0gdpo)\
**Post date:** [June 17, 2019, 10:52pm UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/1 "2019-06-17T22:52:31Z")

</div>

**jsonPath for test assertion is changing from env to env resulting in test failures**  
ex:  
env1 =

> JSON.parse(responseBody).contents[0].contents[0].contents[0].mainContent[9].contents[0].contents[0].totalNumRecs;

env2 =

> JSON.parse(responseBody).contents[0].contents[2].contents[0].mainContent[2].contents[1].contents[0].totalNumRecs;

**I tried below but no luck. Classes and Types are returned in Json response.**

> JSON.parse(responseBody).contents[0].contents[2].contents[0].mainContent[’@class=yy’].contents[1].contents[’@type=XX’].totalNumRecs;

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [June 19, 2019, 7:17am UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/2 "2019-06-19T07:17:12Z")

</div>

Hi @vn0gdpo

Firstly, any chance you could share a sample of the response bodies from env1 & env2 that include the elements you’re trying to locate.

I think expecting items to be located at an arbitrary location in an array is indeed very brittle, could you consider locating them by some sort of name or ID or anything else unique?

I really like using the [javascript array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) with [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)

For example:

```auto
const body = responseBody && JSON.parse(responseBody);
const childElement = body.contents
   .find(item => item.name === `parentName`)
   .find(item => item.id === `childId`);

```

The concatenated `find()`'s will find the element with id=childId inside parent with name=parentName.

You can pretty much do anything inside the arrow functions to work out what element you want to select.

I’ll watch out for your sample data in case the above doesn’t get you going.

---

<div class="post-metadata">

**Author:** ![vn0gdpo](https://avatars.discourse-cdn.com/v4/letter/v/71e660/32.png) [@vn0gdpo](https://community.postman.com/u/vn0gdpo)\
**Post date:** [June 19, 2019, 9:17pm UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/3 "2019-06-19T21:17:35Z")

</div>

Hi @VincentD123,

Example:  
[Env1](https://jsoneditoronline.org/?id=7640646c4f474456b57422cd30a51fc0): [JSON Editor Online: edit JSON, format JSON, query JSON](https://jsoneditoronline.org/?id=7640646c4f474456b57422cd30a51fc0)

[Env2](https://jsoneditoronline.org/?id=03e928d0a50744ef9db55ba956dd116d): [JSON Editor Online: edit JSON, format JSON, query JSON](https://jsoneditoronline.org/?id=03e928d0a50744ef9db55ba956dd116d)

My jsonPath for Env1 is

> object.contents[0].contents[0].contents[0].mainNavigation[2].contents[0].contents[0].navigation[3].numRefinement;

it changes for Env2

> object.contents[0].contents[0].contents[0].mainNavigation[2].contents[0].contents[0].navigation[0].numRefinement;

**InShort, I’m looking for numRefinement value where dimensionName is Brand.**

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [June 20, 2019, 8:12am UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/4 "2019-06-20T08:12:47Z")

</div>

cool. I like the `jsoneditoronline.org` web site, going to use that next time I need to share some JSON 🙂

I was able to locate your `numRefinements`value in both env’s using

```auto
object.contents.find(c => c[`@type`] === `BananaPage`)
   .mainNavigation.find(m => m[`@type`] === `ContentSlotSecondary`)
   .contents.find(c => c[`@type`] === `ContentSlotSecondary`)
   .contents.find(c => c[`@type`] === `GuidedNavigation`)
   .navigation.find(n => n.dimensionName === `Brand`)
   .numRefinements

```

I did notice that the path to `"dimensionName" : "Brand"` for env1 is  
`object►contents►0►mainNavigation►1►contents►0►contents►0►navigation►0►dimensionName`  
which isn’t the same path as you gave, but there was only one key/value pair like that so went with that instead.

I do assume that the value for `@type` is a good unique identifier to use to find the item in the `contents` and `mainNavigation` arrays. If it isn’t, you may want to select by another field.

Lastly, I was able to refine the command using the Chrome Dev Tools.

I created two variables

```auto
const env1 = <<...paste env1 JSON...>>
const env2 = <<...paste env2 JSON...>>

```

Then, as you build up the command, Chrome shows you the results in grey as you go

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/7/e7cd6a3632eea758a1462f9bc8c70aa578b8954f.png)  
You can flick between env1 & env2 results by changing the first variable, a handy way to confirm the command works for both env’s.

---

<div class="post-metadata">

**Author:** ![vn0gdpo](https://avatars.discourse-cdn.com/v4/letter/v/71e660/32.png) [@vn0gdpo](https://community.postman.com/u/vn0gdpo)\
**Post date:** [June 20, 2019, 10:07pm UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/5 "2019-06-20T22:07:00Z")

</div>

@VincentD123 You are my Man !! 🙌  
With some changes I was able to get this script working. I will change all the existing scripts with similar logic to make it stable.  
Thanks Again !!

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [June 21, 2019, 6:22pm UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/6 "2019-06-21T18:22:59Z")

</div>

Hey @vn0gdpo - if @VincentD123 helped to resolve your issue, feel free to mark his post as a **`Solution`** to help someone reading this thread more quickly resolve their own issues in the future 👍

---

<div class="post-metadata">

**Author:** ![james.malvi](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@james.malvi](https://community.postman.com/u/james.malvi)\
**Post date:** [June 27, 2019, 5:04am UTC](https://community.postman.com/t/how-to-handle-dynamic-jsonpath/5949/7 "2019-06-27T05:04:37Z")

</div>

Please make sure to validate JSON data before using it, I use these tools to do that.

1. [https://jsonformatter.org](https://jsonformatter.org)
2. [https://codebeautify.org/jsonvalidator  
 ](https://codebeautify.org/jsonvalidator)
