1

I have below JSON response coming from API, in which 104 and 2 are dynamically changing and I have these values already set in environmental varibales, e.g. {location_id} = 2, {account_id} = 104 Can anyone help on how to parse JSON and get the location object based on environment variable value for location id in postman tests section

{
    "104": {
        "2": [
            {
                "FirstName": "John",
                "LastName": "McClain",
                "Phone": "1234567890"
           }
        ],
        "3": [
            {
                "FirstName": "Rita",
                "LastName": "Maria",
                "Phone": "3092432345"
           }
        ]
    }
}
3
  • Could you clear up the question a little bit - I'm finding it different to see what you're asking. Where are you saving those env vars from? Commented Feb 28, 2018 at 16:27
  • Danny, i need to parse the above JSON but the keys like 2,3, are going to be dynamic, meaning they are based on a previous other API response. Commented Mar 5, 2018 at 13:04
  • Yeah I understand that - What are you trying to do with the parsed data. Are you trying to assert against the values of that? Commented Mar 5, 2018 at 13:47

1 Answer 1

1

This is a very horrible and flaky way to 'test' the values from the response JSON in the question. This assumes that the environment vars that were set in the previous request were account_id = 104 and location_id = 3. This does a hardcoded check to see that the FirstName property in that object, equals 'Rita'.

pm.test('Get the values', () => {
    var jsonData = pm.response.json()[pm.environment.get('account_id')][pm.environment.get('location_id')]
    pm.expect(jsonData[0].FirstName).to.equal('Rita')
})

It's difficult to tell what you actually want to do with the data and this test isn't something that I would use but if you just wanted an insight into how you would parse the data based on some environment vars this is at least a starting point.

If more information is provided, I will update my answer to reflect this.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually I am learning about postman scripting and was stuck to get object from response for writing further tests in the same script. All I needed was to get this object: var jsonData = pm.response.json()[pm.environment.get('account_id')][pm.environment.get('location_id')] Thanks it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.