title: "Replicating Working API Submission With Nested Json Object Using Postman" date: "2018-11-12" coverImage: "Screenshot-2018-11-12-at-11.18.21-AM.png"
I am fond of using Postman to test against my API backend. Today, I have a particular problem while testing because I have a nested Json object as part of my request payload.
The specific problem is my usual way of replicating a working API submission couldn't properly recreate a nested Json object.
I overcome that and found a couple of new tricks I want to share in this article.
Therefore, in this article, I will :
Let's begin!
In Postman, I typically copy and paste the values of the Request Payload from Chrome Dev Tools into Params section of the Postman form.
[caption id="attachment_339" align="alignnone" width="800"] Figure 1: Request Payload in Chrome Dev Tools[/caption]
[caption id="attachment_340" align="alignnone" width="600"] Figure 2: Params section in Postman[/caption]
Usually I will type in one key-value pair at a time. Now, let's use a different example to illustrate how you can type in the nested Json object one at a time.
Suppose your raw Json is this:
"Items": [ { "sku": "9257", "price": "100" } ]
You can type directly into the key-value interface this way.
[caption id="attachment_351" align="alignnone" width="656"] Figure 3: An example of using the key-value interface in Postman. Image courtesy via StackOverflow[/caption]
If I were to do the same for the deeply nested Json object in Figure 1, it will be tedious and time-consuming. Is there a better way to do this?
This leads us to Part 2 where I show you how you can quickly copy paste existing payload from Chrome to Postman.
Here's a quick tip about copy pasting from Chrome Dev Tools to Postman that isn't obvious to most users.
You can click the view source in Request Payload inside Chrome Dev Tools. Copy the entire source and directly paste into the Params section of Postman.
[caption id="attachment_341" align="alignnone" width="600"] Figure 4: After switching the Request Payload to raw Json string[/caption]
[caption id="attachment_346" align="alignnone" width="600"] Figure 5: Copy pasting from Chrome to Postman Params (Neat trick!)[/caption]
I have shown in Part 1 how to type in nested Json directly into the key-value pair interface which Postman provides. This method is fine if your payload has only a few key-value pairs. However, it can be time-consuming and error-prone for a complicated payload.
Therefore, in Part 2, I've shown a faster and more accurate way to replicate a complicated payload. However, this creates another issue.
When I use the copy-paste method, and I happen to have nested Json object in my values, the nested object will come out as [object Object] as you can see here.
[caption id="attachment_343" align="alignnone" width="600"] Figure 6: Nested Json object in Postman showing up in Params (You don't want this)[/caption]
Let me recap thus far:
So is there a better way that can give us the best of both worlds? Where we can replicate an existing working API submission quickly with all the rich details of the nested Json object? The answer is in Part 3.
So these are the exact steps to send out nested Json object in your request payload in Postman.
Make sure your Headers contain a Content-Type
and Accept
with the values application/json
[caption id="attachment_344" align="alignnone" width="600"] Figure 7: Headers should look like this in Postman[/caption]
3. In Body, choose raw and JSON(application/json) and then paste your raw payload in the Json string format into it. In my case, I simply repeated the same copy and paste routine except this time I pasted the payload into this section.
[caption id="attachment_345" align="alignnone" width="800"] Figure 8: Body should look like this in Postman[/caption]
As you can see from Figure 8, the nested Json object is clearly visible with all the intricacies in it.
In conclusion, these 3 steps should ensure that you are sending nested Json objects as part of your request payload successfully. And if you want to prettify the raw Json string, you can click on the Beautify link on the right.
If you want to still use Postman's key-value interface, I recommend it provided that:
Otherwise, you're better off with the approach using the raw Body in Postman as explained in Part 3.
In conclusion, I have shown you two ways to enter nested Json object:
In addition, I have shown you a trick on how to copy paste payload info from Chrome to Postman.
Did I miss out anything with regards to nested Json object in Postman? Let me know in the comments below!
I intend to make every page in my blog authoritative on the topic they are written about. So, I appreciate every helpful comment.