Child Properties of Data Variables

Hi I’m trying to use a Data File to replace property values in the Collection Runner. My problem is I can’t figure out how to access the Child property of a Property with a Data file variable when I run the collection.

This is my Data File:

[
    {
        "firstname" : "Alice",
        "lastname" : "Smith",
        "totalprice" : 300,
        "depositpaid" : true,
        "bookingdates" : {
            "checkin" : "2018-01-05",
            "checkout" : "2019-01-05"
        },
        "additionalneeds" : "Breakfast"
    },
    {
        "firstname" : "aasDDFFasdfasDfFAsdf",
        "lastname" : "Yule",
        "totalprice" : 100,
        "depositpaid" : true,
        "bookingdates" : {
            "checkin" : "2018-01-01",
            "checkout" : "2019-01-01"
        },
        "additionalneeds" : "Breakfast"
    },
    {
        "firstname" : "Brad",
        "lastname" : "120934839212314",
        "totalprice" : 11900,
        "depositpaid" : false,
        "bookingdates" : {
            "checkin" : "2018-01-01",
            "checkout" : "2019-01-01"
        },
        "additionalneeds" : "Breakfast"
    }
]

This is my collection I’m experimenting in:
https://www.getpostman.com/collections/8d4d016855291dafb773

If you run the collection, the response body gives “Invalid Date”.
Tried changing the variable to loads of stuff like:

"{{bookingdates}}.checkin"
"bookingdates.{{checkin}}"

And it ain’t happening! I get stuff like [Object object].{{checkin}} but it won’t take the “2018-01-01”, for example, from the Data File no matter what syntax I try out for the value’s address.

Any advice? I’ve looked over https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/, but this only shows an example of a flat data file.

Additionally: I’m guessing it’s going to be even more of a pain to figure out how to do this if the Data File contains arrays too, so any example of how to handle that would also be appreciated.

Thanks.

The way that I would do it, it might not be the right or only way, is to add a script to grab that data first, then use JSON.stringify() and set it as a new variable. Not the ideal solution but it will work.

First add this to your Pre-request Script:

pm.variables.set("dates", JSON.stringify(pm.iterationData.get("bookingdates")))

Then add this syntax, with the new variable name, in the POST request body. It will send that data through the runner

{
	"firstname" : "{{firstname}}",
	"lastname" : "{{lastname}}",
	"totalprice" : {{totalprice}},
	"depositpaid" : {{depositpaid}},
	"additionalneeds" : "{{additionalneeds}}",
	"bookingdates" : {{dates}}
}

1 Like

Thank you very much, exactly what I needed.
Knowing to use the Pre-Request script to achieve this, I might be able to figure this out for arrays/nested arrays too.

1 Like