Newman: POST with nested JSON obj

Created on 3 Jul 2015  路  19Comments  路  Source: postmanlabs/newman

Newman has problem while doing POST with content type JSON. It doesn't read nested objects.
[ { "user": { "login": "123", "password":"pass" } } }

and in body it looks like this
{ "login":"{{user.login}}", "password":"{{user.password}}" }

But when I make JSON data
[ { "login": "123", "password":"pass" } ]
and body
{ "login":"{{login}}", "password":"{{password}}" }

It works!

Is it possible to use nested JSON obj while doing POST?

feature request tracked

Most helpful comment

I was facing the same problem with nested objects in Postman - not sure if it applies to the problems mentioned above but this is what I did.

  1. Added in the Pre-request Script
var temp = JSON.stringify(data);
postman.setGlobalVariable("temp", temp);
  1. In the body:
    {{temp}}

Hope it works for someone else too.

All 19 comments

To send raw JSON data, you need to use dataMode: raw, and set the rawModeData property.

Check https://www.getpostman.com/collections/171ad15d3c38f76f5193

@abhijitkane
I'm doing it as you said. It works in Postman, but it doesn't in Newman.
The main problem is to pass nested JSON in POST.

Are you using the -d option in Newman to use JSON data files?
If so, only files like [ { "login": "123", "password":"pass" } ] are supported. CSV is also supported, and there's no easy way to include nested objects in CSV files.

Yes, I'm using -d to include JSON file with data.
Nested objects works in test scripts but they don't in rawModeData.

In postman everything works fine, only newman got this issue.

Is there any chance to update newman with this feature?

I would like this feature as well! Any progress or target date?

What would be a sure-shot way to reproduce this? Maybe a scaled down collection and other details would help.

@irhabi Apologies for the delay, but could you try with latest release of Newman (v3.1.1), and let us know how that works for you?

@kunagpal can you reproduce this issue based on your interpretation of the original issue? Maybe also let know of what's interpreted. If you cannot reproduce, then let's close this until we have more details.

@irhabi @ChristianLegault @shamasis Based on our current interpretation, this is what the situation looks like:
You wish to use nested data variables, in JSON format, but the variables are not resolved. This has been confirmed for both, Newman v3.2.0, and Postman v4.8.0, as nested JSON variables are not supported (for any kind of request)... yet.

@kunagpal it would be very nice to have this feature soon.

I would like this feature as well! Any progress or target date?

I'm having the same problem when running my collection from newman
in one test i try to post with JSON nested objects.
data.json ex:
[
{
"name": "john doe",
"address": [
{
"address": "address example",
"postcode": "123",
"type": "HOME"
},
{
"address": "address example",
"postcode": "456",
"type": "OFFICE"
}
]
}
]

When i see the server log file this request body is received like this:
{"name": "john dow", "address": [Object, Object]}

In postman my request body looks like this:
{
"name": "{{name}}",
"address": "{{address}}"
}

Using newman v. 3.3.0

@fasatrix @allienn There's no E.T.A on this as of now, but we'll be keeping this thread updated with any progress that occurs :smile:

Any update on this issue? I am also facing similar problem.

I am also facing similar problem https://github.com/postmanlabs/newman/issues/1136

In the request body, I need to use something like your question:
"and in body it looks like this
{ "login":"{{user.login}}", "password":"{{user.password}}" }"

Any update?

Please, thx

I was facing the same problem with nested objects in Postman - not sure if it applies to the problems mentioned above but this is what I did.

  1. Added in the Pre-request Script
var temp = JSON.stringify(data);
postman.setGlobalVariable("temp", temp);
  1. In the body:
    {{temp}}

Hope it works for someone else too.

@kunagpal can you please provide closing reasons? i.e link to the app repo issue where this resides, etc

I am facing this issue in 2020. Strange!!!

If I do the following in the Pre-request Script it will work in both Postman and Newman:

// The following is needed to interoperate between Postman and Newman
var iterationData = pm.variables.get("requestBody");
if (pm.iterationData.get("requestBody") != null) {
    iterationData = JSON.stringify(pm.iterationData.get("requestBody"));
}

pm.globals.set("iterationData", iterationData);

I would like to avoid having to do this - does anyone have any idea how I can?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

al-tr picture al-tr  路  4Comments

brimchan picture brimchan  路  3Comments

AlphaWong picture AlphaWong  路  3Comments

Pixcell picture Pixcell  路  3Comments

DotNetRockStar picture DotNetRockStar  路  5Comments