How can I target nested objects from a JSON data file?

Hey,
I’ve always used CSVs and want to experiment using JSON files in the collection runner.
Given I have the below JSON data file

[
    {
        "email": "[email protected]",
        "address": {
            "addressLine1": "123 Fake Street",
            "county": "Something",
            "postcode": "BA11 4PM"
        }
    }
]

And I want to use the data file in my POST body each iteration (I know, I only have one object so it’ll iterate once which is fine for now)
So the raw JSON data that will be posted to the endpoint will be:

[
    {
        "email": "[email protected]",
        "address": {
            "addressLine1": "{{address.addressLine1}}",
            "county": "Something",
            "postcode": "BA11 4PM"
        }
    }
]

“{{address.addressLine1}}” is literally sent to the server instead of the revelent value in the JSON data file, I’m expecting it to be “123 Fake Street”

Am I missing something stupidly obvious here?

Argh! Looks like this is not something Postman can do??
:frowning:

1 Like

Yeah. Nested JSON structures are not supported right now.

Is there any progress on this issue?
I can see some changes mades to the feature request in Github

@abhinav

Any idea if this functionality has been added in the last few years? From the things I’m trying, it appears that nested JSON in external files still doesn’t work - but I might be missing something.

Do you know of any way to bring nested JSON in from an external file maybe as one big string and then parse it in a pre-req script?

I figured out a way to do this:

  • in your input JSON file, add a parent object that contains all of your nested JSON
  • create a local variable from the nested JSON in the data file in the pre-request: var inputJSONdata = pm.iterationData.get(“parentObject”);
  • create a stringified environmental variable: pm.environment.set(“envInputJSONdata”,JSON.stringify(inputJSONdata));
  • now you can work with the nested elements of that - for instance, you can populate the request body with the entire variable by putting {{envInputJSONdata}} in the Body
3 Likes

This response saved me so much of heartache an pain.
Thank you for this.

1 Like

Hi, I have the exact same requirement. I am not able to follow your steps. Can you help me how to read the nested json from file . This is how actual JSON looks like in request.

Highlighted in bold is the section that I need to read from the JSON file.

[
{
    "type": "report",
    "config": {
        "reportId": "6d45744b-2fb4-46b1-8ffb-bad96793bc99",
        "outputFormat": "PDF"
    },
    **"Selections": [**
**        {**
**            "FieldName": "FilterBusinessPartyId",**
**            "SelectedCount": 2,**
**            "SelectedValues": [**
**                "100046760",**
**                "100064485"**
**            ],**
**            "IsNumeric": true**
**        },**
**        {**
**            "FieldName": "ChildDivisions",**
**            "SelectedCount": 1,**
**            "SelectedValues": ["All Divisions"],**
**            "IsNumeric": false**
**        }**
**    ]**
	,
    "connectionId": "b5e1b49e-3539-45f4-8c98-26e1be0fdf93"
}
]

Are nested json data files now supported in 2024?

Hey @cruxto :wave:

As it’s been a while since the last post in this thread :sweat_smile: - Did you want to explain the usage of this in your context?

Hi @danny-dainton, Yes I know I was surprised it’s still open :slight_smile:

One scenario I would like to use with is data driven tests where you need more than an a one to one mapping.

E.g. you have an object, and in some iterations you want to add only one item to the array, but in other iterations you may want to add say 3 items for example.

So, if you could add an array of objects into the request that would be really helpful.

Currently, we are creating workarounds using a csv file using some sort of other delimiter like a pipe in a field and processing it in the pre-request to generate the dynamic playload.

But it’s not ideal, as it not easy for less experienced people on the team to work with.

Hope this make sense,
Cheers

1 Like

@danny-dainton, so is it the case that nested JSON is still not supported natively with Postman?

@cruxto

I’ll let someone from Postman give you the specific answer, but I can’t see how this would work efficiently.

To parse Nested\Complex JSON properly, you would need a lot of code to take into consideration all of the possible combinations. Arrays within arrays, stringified objects, etc.

At the moment, each iteration is a single object in your JSON, or a single line in the CSV file. This makes it easy to write code to target those elements.

I’m not saying that the feature couldn’t be developed, but I can see it being fairly complicated.

At it stands, you can have complex JSON, but you will need to parse it yourself in a pre-request script where you can set variables for the current request, or you could replace some or all of the body using “pm.request.body.raw”.

I guess if you really want this feature, the first step is to raise it on the Postman Github.

2 Likes

@michaelderekjones thank you so much for that – that’s a perfect answer. In general, we like to use GitHub for feature requests so that we can more easily track, triage, and prioritize them.

We have begun exploring using JSON filtering (as an example: JMESPath, which is a bit like jq or JSONPath. We’d thought of this primarily to navigate response bodies, but perhaps this functionality could be useful in other areas of the product.

If you open that issue, we’ll reply and we’d love to further understand the use case and how we can assist.

Dustin

2 Likes

@michaelderekjones Thanks for responding.

I’m sure it can get really complicated, pretty quickly with all the possible combinations.

For now, I’ll continue building up a request body as part of the pre-request and add the new variable to request body.

Thanks again
Paul.

1 Like