Using 1 json file with multiple arrays as input for different requests

I have a collection with multiple post requests (create account, create address etc.) and a json file as input that looks like this:

Json data file

{
    "testdata": {
        "accountlist": [
            {
                "bankname": "BARCLAYS BANK UK PLC",
                "accountnumber": "00004005000"
            },
            {
                "bankname": "Bank of America",
                "accountnumber": "00004005001"
            },
            {
                "bankname": "Citigroup",
                "accountnumber": "00004005002"
            },
            {
                "bankname": "Wells Fargo",
                "accountnumber": "00004005003"
            },
            {
                "bankname": "U.S. Bancorp",
                "accountnumber": "00004005004"
            },
            {
                "bankname": "PNC Financial Services",
                "accountnumber": "00004005005"
            }
        ],
        "adresslist": [
            {
                "city": "Edinburg",
                "street": "14 Nile Grove"
            },
            {
                "city": "Copenhagen",
                "street": "Frederiksborggade 12"
            },
            {
                "city": "London",
                "street": "Piccadilly 421"
            }
        ]
    }
}

POST Create Account body

{
    "bankname": "{{bankname}}",
    "accountnumber": "{{accountnumber}}"
}

POST Create Address body

{
    "city": "{{city}}",
    "street": "{{street}}"
}

How can I get the testdata.accountlist array into the POST Create Account as input for each iteration and the testdata.addresslist into the POST Create Address as input for each iteration?

My recommendation would be to transform your test data before it gets used in the Postman collection runner, then you wouldnโ€™t need to make any changes and it should run as you currently have variables defined.

Postman requires the JSON to be a straight up array like the following.

[
	{
		"bankname": "BARCLAYS BANK UK PLC",
		"accountnumber": "00004005000"
	},
	{
		"bankname": "Bank of America",
		"accountnumber": "00004005001"
	},
	{
		"bankname": "Citigroup",
		"accountnumber": "00004005002"
	},
	{
		"bankname": "Wells Fargo",
		"accountnumber": "00004005003"
	},
	{
		"bankname": "U.S. Bancorp",
		"accountnumber": "00004005004"
	},
	{
		"bankname": "PNC Financial Services",
		"accountnumber": "00004005005"
	}
]

What Iโ€™m not quite getting is that your address list is a completely separate array, so which address should be linked to which bank? You have 6 banks, but 3 addresses.

Personally, I would recommend adding the city and street under the bank name. Keeping the JSON as flat\simple as possible.

If they are completely separate requests, then you will probably need to run these as separate requests in the runner.

You could have the JSON as single string as the only entry in a CSV file, and then use pre-request scripts to retrieve the data and set collection\environment variables for the two different arrays and then create a loop using array shift and setNextRequest but that is a fair bit of code which wouldnโ€™t be needed if the test data was in the correct format. (Just giving that option in case you canโ€™t change the format of the test data).

The two lists have nothing to do with each other. The list of banks is input for one request whereas the lists of banks is input for another. So it is indeed the pre-request script required to โ€œfetchโ€ the appropriate array in each request I am looking for.

It would be fairly easy if a specific file could be assigned to each of the different requests rather than only have one file for the entire collection. I could of course make a new collection for each requests, but then the idea of a collection sorta goes away :slight_smile: