1

I have a JSON formatted stream, full of objects. Each object looks like this:

{
  "object": "alpha",
  "attributes": [
    {
      "type": "A",
      "description": "a",
      "value": 1271129046.9144535
    },
    {
      "type": "B",
      "description": "b",
      "value": 6738889338.63777
    },
    {
      "type": "C",
      "description": "c",
      "value": 214918692.38456276
    },
    {
      "type": "D",
      "description": "d",
      "value": 140222346.75136077
    },
    {
      "type": "E",
      "description": "e",
      "value": 2085635554.8128803
    }
  ]
}

I'd like to get data out as:

alpha,A,a,1271129046.9144535
alpha,B,b,6738889338.63777
alpha,C,c,214918692.38456276
alpha,D,d,140222346.75136077
alpha,E,e,2085635554.8128803

The next object may be "beta" instead of "alpha", hence I don't want to just strip the "object" key.

My restrictions are that I want to process this stream in a bash pipeline. I'm hoping I can just use "jq" for this, rather than piping through python/ruby/perl etc which I'd rather not depend on if I can help it.

Any ideas would be most grateful!

2 Answers 2

3

It looks like you're building up CSV data, the @csv filter was made for this. You just need to collect an array of the values you want to write out and pass it in to the filter. You could do this:

$ jq -r '.attributes[] as $attr | [.object, $attr.type, $attr.description, $attr.value] | @csv' input.json

Which produces this:

"alpha","A","a",1271129046.9144535
"alpha","B","b",6738889338.63777
"alpha","C","c",214918692.38456276
"alpha","D","d",140222346.75136077
"alpha","E","e",2085635554.8128803
Sign up to request clarification or add additional context in comments.

1 Comment

Yet another feature I wasn't aware of in jq! It's a veritable smorgasbord of utility!
0

(1) Slightly briefer than the accepted answer:

jq -r '[.object] + (.attributes[] | [.type, .description, .value]) | @csv'

(2) If you don't want the quotation marks, then one possibility would be:

jq -r '"\(.object)," + (.attributes[] | "\(.type),\(.description),\(.value)")'

Comments

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.