How to handle dynamic jsonPath

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;

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 with arrow functions

For example:

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.

1 Like

Hi @VincentD123,

Example:
Env1: JSON Editor Online: edit JSON, format JSON, query JSON

Env2: JSON Editor Online: edit JSON, format JSON, query JSON

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.

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

I was able to locate your numRefinementsvalue in both env’s using

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

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


You can flick between env1 & env2 results by changing the first variable, a handy way to confirm the command works for both env’s.

1 Like

@VincentD123 You are my Man !! :raised_hands:
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 !!

1 Like

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 :+1:

1 Like

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

  1. https://jsonformatter.org
  2. https://codebeautify.org/jsonvalidator
1 Like