Maulik Pipaliya Joyy
Maulik Pipaliya Joyy

Reputation: 344

jq : print key and value for each entry in nested object

This is JSON Object

{
    "success": true,
    "terms": "https://coinlayer.com/terms",
    "privacy": "https://coinlayer.com/privacy",
    "timestamp": 1620244806,
    "target": "USD",
    "rates": {
      "611": 0.389165,
      "ABC": 59.99,
      "ACP": 0.014931,
      "ACT": 0.021098,
      "ACT*": 0.017178,
      "ADA": 1.460965
    }
  }

I require this type of output:

611,0.389165
ABC,59.99
ACP,0.014931
ACT,0.021098
ACT*,0.017178
ADA,1.460965

Can somebody help me figure out doing it preferably with jq, shell script or command.

Upvotes: 0

Views: 1958

Answers (1)

Shawn
Shawn

Reputation: 52334

You can use @csv to generate CSV output from arrays, and to_entries to break up the object's elements into said arrays:

$ jq -r '.rates | to_entries[] | [ .key, .value ] | @csv' input.json
"611",0.389165
"ABC",59.99
"ACP",0.014931
"ACT",0.021098
"ACT*",0.017178
"ADA",1.460965

Upvotes: 4

Related Questions