Drill down in JSON response when an object starts with a number

Hi all,

I searched for an answer to my question but I could not find one. Sorry if I missed it.

I have JSON data that looks like this:

{
    "someInfo": {
        "size": -20.7,
        "class": "fCls-blb2ff5z0o",
        "150Stuff": {
            "title": "no",
            "identifier": "no"
        },

In my pre-Request Script, I set ‘150Stuff’ to yes or no randomly.

I want to test that ‘150Stuff’ in the response matches what’s in my variable (either yes or no). I parse the JSON first:

var jsonDataNewJob = pm.response.json();

Then I have a test that looks like this:

pm.test('150Stuff matches variable', function () { 
   pm.expect(jsonDataNewJob.someInfo.150Stuff.title).to.eql(pm.environment.get('150Stuff'));
});

Postman doesn’t like the ‘150Stuff’ in the line: jsonDataNewJob.someInfo.150Stuff.title and shows the error:

Unmatched '{'.

Any idea how I can get Postman to drill down to jsonDataNewJob.someInfo.150Stuff.title without error?

Thanks so much in advance!
Ed

Hey @ed_truqc,

Not really sure I 100% understand what you’re doing in the Pre-request Script and how that relates to the response or the environment variable. How are you setting the environment variable?

The JSON example you have added to the question isn’t valid and is missing 2 }'s to close the ones that are opened, in the script.

As for the Test, to access that value you would need to do this and wrap the 150Stuff property in brackets:

jsonDataNewJob.someInfo["150Stuff"].title
2 Likes

Hi @danny-dainton. Thanks so much for this reply. Worked like a charm! I think I tried every other way to make this work :smile: I really appreciate the help!

Have a great weekend,
Ed

1 Like

You’re very welcome. :trophy: