Question

JSON response formatting

I’m trying to built an API that uses different databases to show recent news articles. It’s nothing for client use, just trying it out for a couple of friends.

I’m looking for something that will help me visualize it a bit better in their console. They are new to all this technical stuff and I was searching for something to help them out.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 30, 2022
Accepted Answer

Hello,

I’m not sure I fully understand. Do you need to make it as a GUI or do you need them to see it in a terminal but sorted and sortable?

If you are looking for the latter, I’ll recommend using the JQ package. jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed , awk , grep and friends let you play with text. jq is written in portable C, and it has zero runtime dependencies

It sounds a lot harder than it is actually. Imagine you call your API with CURL or whatever you are using, you just need to pipe it with jq and that’s it.

Here is an example. Now, as I don’t have an actual JSON at hand, I’ll echo one for myself:

  1. echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq '.'
  2. {
  3. "fruit": {
  4. "name": "apple",
  5. "color": "green",
  6. "price": 1.2
  7. }
  8. }

Let’s assume you want to get only the name of the fruits:

  1. echo '{"fruit":{"name":"apple","color":"green","price":1.20}}' | jq .fruit.name
  2. "apple"

If you had more information in the JSON you would’ve seen more information in there.

Hope this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel