19

I want to send a JSON request but problem is I need to send my userPropertiesAsJsonString field as JSON string.

How can I send userPropertiesAsJsonString as JSON string?

{
    "User" : {
        "userId" : "11111",
        "userPropertiesAsJsonString" : ?
    }
}

userPropertiesAsJsonString is;

{
    "properties" : {
        "propertyName" : "test",
        "propertyDesc" : "desc"
    }
}
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • What's the problem with pasting what you posted here in Postman? Should work if I'm not mistaken. Just make sure the quotes are escaped with `\"` instead of just `"` – Kevin Jan 18 '18 at 08:55
  • If this is in JavaScript, can’t you just do `JSON.stringify(userPropertiesAsJsonString)` there? – Sebastian Simon Jan 18 '18 at 08:55

5 Answers5

32

Try this :

{
    "User" : {
        "userId" : "11111",
        "userPropertiesAsJsonString" : "{\"properties\" : {\"propertyName\" : \"test\",\"propertyDesc\" : \"desc\"}}"
    }
}
sanatsathyan
  • 1,713
  • 12
  • 18
23

Jason Mullings' answer did not work for me, but it was an excellent base that allowed me to come up with a solution to a problem very similar to yours.

In the Pre-request Script tab,

const userPropertiesAsJsonString = {
    "properties" : {
        "propertyName" : "test",
        "propertyDesc" : "desc"
    }
}

pm.environment.set(
    'userPropertiesAsJsonString',
    JSON.stringify(JSON.stringify(userPropertiesAsJsonString))
);

Then, in the Body tab,

{
    "User" : {
        "userId" : "11111",
        "userPropertiesAsJsonString" : {{userPropertiesAsJsonString}}
    }
}

Stringifying the userPropertiesAsJsonString variable twice will allow you to escape the JSON string (solution obtained from this answer; refer to this gist for a more detailed explanation) which will then allow you to obtain a request body that looks like the one in the answer provided by sanatsathyan.

sabriele
  • 586
  • 6
  • 14
8

pre-request script:

let query = {}

pm.environment.set('query', JSON.stringify(query));

body:

{{query}}
Jason Mullings
  • 850
  • 14
  • 10
2

As JSON means JavaScript Object Notation, so you can just copy the userPropertiesAsJsonString into the original JSON:

{
    "User" : {
        "userId" : "11111",
        "userPropertiesAsJsonString" : {
            "properties" : {
                "propertyName" : "test",
                "propertyDesc" : "desc"
            }
        }
    }
}

Copy and paste this JSON into the Postman request body (raw formatted) and set the header "Content-Type: application/json".

If you have to do more fancy stuff before the request you can execute a pre-request script in Postman: https://www.getpostman.com/docs/postman/scripts/pre_request_scripts

For more about JSON see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

dwettstein
  • 667
  • 1
  • 7
  • 17
  • This is what i would go for, just use the original JSON and not a string - As it's using Postman to `POST` the request payload, `JSON.stringify()` won't work as the app will fail the validation as it checking that the schema is valid JSON. – Danny Dainton Jan 18 '18 at 09:20
1

Similar to other answers but:

pm.variables.set('myJsonAsEscapedString',JSON.stringify(`{"user_id": 1, "name": "Jhon"}`))

That way you can use it without escaping explicitly.

alexaner
  • 11
  • 1