Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send JSON object as JSON String with Postman?

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"
    }
}
like image 937
hellzone Avatar asked Jan 18 '18 08:01

hellzone


People also ask

How do I convert a JSON object to a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

Does postman Stringify JSON?

Postman builds and stores this variable for use between portions of the application, and may execute code and build things in an order or manner that you are not expecting. Postman does its own serialization of the data, since we cannot know what you intend to do with your data. By using JSON. stringify() and JSON.

How do I use userpropertiesasjsonstring in a postman request?

As JSON means JavaScript Object Notation, so you can just copy the userPropertiesAsJsonString into the original JSON: Copy and paste this JSON into the Postman request body (raw formatted) and set the header "Content-Type: application/json".

Why does jersey read postman's JSON form data as string?

Since the request sent from PostMan doesn't contain content type for JSON form data parameter, Jersey read it as String rather than a JSON object. The request content from post man is: `Cache-Control: no-cache. Postman-Token: 5241a928-78e9-2b70-637a-96a68775c85b.

Is postman Console data being stored in a JSON object?

When I examined the data in the Postman Console, it appeared that they were being stored in a JSON object, yet I was unable to access the values correctly on subsequent requests. For viewers of my Postman Blindfold Challenge, we all learned that I love to peel back the layers on what’s really going on inside a system.

How to put JSON data in a text file?

The json part of the body should also be set as "File" rather then "Text", and put your json data in a json file for example "a.json". Just find out that this method doesn't work on windows, but works fine on linux.


2 Answers

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.

like image 26
sabriele Avatar answered Sep 17 '22 05:09

sabriele


Try this :

{
    "User" : {
        "userId" : "11111",
        "userPropertiesAsJsonString" : "{\"properties\" : {\"propertyName\" : \"test\",\"propertyDesc\" : \"desc\"}}"
    }
}
like image 178
sanatsathyan Avatar answered Sep 19 '22 05:09

sanatsathyan