0

I have a JSON payload that I'm looking to convert to CSV that looks like the below:

[
  {
    "endpoint": "APPLE",
    "date": "2022-11-02 12:00",
    "upUsage": 0,
    "downUsage": 18000,
    "upAvgRate": 0,
    "downAvgRate": 600,
    "upMaxRate": 0,
    "downMaxRate": 800
  },
  {
    "endpoint": "BANANA",
    "date": "2022-11-02 12:00",
    "upUsage": 0,
    "downUsage": 17600,
    "upAvgRate": 0,
    "downAvgRate": 587,
    "upMaxRate": 0,
    "downMaxRate": 693
  },
  {
    "endpoint": "CARROT",
    "date": "2022-11-02 12:00",
    "upUsage": 0,
    "downUsage": 8000,
    "upAvgRate": 0,
    "downAvgRate": 533,
    "upMaxRate": 0,
    "downMaxRate": 533
  }
]

I am trying to convert this to a standard CSV file with the appropriate headers via jq, but having difficulties in doing so. Below is my desired output:

"endpoint","date","upUsage","downUsage","upAvgRate","downAvgRate","upMaxRate","downMaxRate"
"APPLE","2022-11-02 12:00",0,18000,0,600,0,800
"BANANA","2022-11-02 12:00",0,17600,0,587,0,693
"CARROT","2022-11-02 12:00",0,8000,0,533,0,533

I've been able to use the below jq to get close to this output, but my headers are not being included:

cat testJson.json | jq -r '.[] | join(",")'

*Note: - There are also instances in which one of my JSON objects may not include the same number of values, so I need my output file to account for this and simply enter a null value between the commas to keep a consistent number of columns

2
  • 1
    Does this answer your question? How to convert arbitrary simple JSON to CSV using jq? Commented Nov 2, 2022 at 16:35
  • The command you've tried does nothing with the headers. Please take a look at the marked duplicate, that should be just fine to convert your json to csv. Commented Nov 2, 2022 at 16:36

1 Answer 1

1

Assuming the keys are consistently ordered within the objects:

jq -r '((first | keys_unsorted), (.[] | to_entries | map(.value))) | @csv' file.json
"endpoint","date","upUsage","downUsage","upAvgRate","downAvgRate","upMaxRate","downMaxRate"
"APPLE","2022-11-02 12:00",0,18000,0,600,0,800
"BANANA","2022-11-02 12:00",0,17600,0,587,0,693
"CARROT","2022-11-02 12:00",0,8000,0,533,0,533
Sign up to request clarification or add additional context in comments.

5 Comments

Could you explain why this needs an answer, instead of closing as duplicate?
Because I worked out an answer before I read the comments, so I shared it. Why do you need an explanation?
@GlennJackman - I believe the question is whether you found anything in the Q or your A that is not already covered by an existing SO Q&A.
This assumes the fields of every object is in the same order, but JSON defines the order meaningless. This program could very well not work, or it could stop working at any time.
Just out of curiosity, I assumed I was missing something @glennjackman.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.