2

In Postman, I want to pass a dynamic JSON string to a variable and use it in a request. This is what I have:

Pre-request:

var myJsonString = "{ \"type\": \"10\", \"number\": \"123456\" }";

pm.variables.set("my-json-string", myJsonString);

Body:

"body":{
     "jsonString":"{{my-json-string}}"
}

But this does not work. Do you know any solution for this?

If I send the request like this, it works perfect:

"body":{
     "jsonString":"{ \"type\": \"10\", \"number\": \"123456\" }"
}

2 Answers 2

1

Have you tried wrapping it with JSON.stringify()?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

pm.globals.set("my-json-string", JSON.stringify(myJsonString))

As this value is saved as a string, you will not need to use the delete double-quotes on variable within the Post body:

"jsonString":{{my-json-string}}
Sign up to request clarification or add additional context in comments.

1 Comment

Does this answer the question?
0

you need to store it using JSON.stringify

let response = pm.response.json(),
jsonVariable = JSON.stringify(response);
pm.environment.set("yourVariable", jsonVariable );

And then use it in another like:

"yourVariable":"{{yourVariable}}",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.