{"id":1,"firstName":"Bilbo","lastName":"Baggins","name":"Bilbo Baggins","role":"burglar","_links":{"self":{"href":"http://localhost:8080/employees/1"},"employees":{"href":"http://localhost:8080/employees"}}}Surely you want to have the JSON content formatted nicely like the below, right?
{ "id": 1, "firstName": "Bilbo", "lastName": "Baggins", "name": "Bilbo Baggins", "role": "burglar", "_links": { "self": { "href": "http://localhost:8080/employees/1" }, "employees": { "href": "http://localhost:8080/employees" } } }In this quick post, I’m going to share with you how to prettify JSON with curl like that - making API testing using curl more convenient and easier.Basically, to format JSON with curl, you need to use pipe syntax in command line as follows:
curl options URI | <another_tool>
Here, | is the pipe character, followed by another tool that processes the output given from curl.curl localhost:8080/employees/1 | jq
The output will look like this: Using NodeJS:Download and install NodeJS from its official homepage. Then open a new command prompt, type the following:npm install -g jsontool
This command will install JSON tool. Then you can pretty print JSON output from curl using the follow command:curl localhost:8080/employees/1 | json
curl localhost:8080/employees/1 | json_pp
or:curl localhost:8080/employees/1 | python -m json.tool
That’s few tips you can use to prettify JSON output when testing REST APIs using curl. I hope you found this post helpful. Thanks for reading.