Jq: Flatten array objects with parent values?

Created on 11 Dec 2014  路  2Comments  路  Source: stedolan/jq

Given:

{"id": 1, "date": "2014-12-30", "history":[{"id":1, "date":"2014-12-30", "type":"open"},{"id":2, "date":"2014-12-31", "type":"close"}]} 

I am attempting to flatten the data for import into RDBMS tables (via the @csv operator), with each object in the "history" array (with 1 or more array elements) as a separate line with the parent fields duplicated, as in:

{"id": 1, "date": '"2014-12-30", "id":1, "date":"2014-12-30', "type":"open"}
{"id": 1, "date": '"2014-12-30", "id":2, "date":"2014-12-31', "type":"close"}

eventually becoming

1, 2014-12-30, 1, 2014-12-30, "open"
1, 2014-12-30, 2, 2014-12-31, "close"

I see a flatten command coming in jq 1.5, but not sure if it or another method is already available in jq 1.4. Noting the "id" field is duplicated in name (as with the data I am receiving), I could either deal with it by position, or accept a prefix assigned by jq.

Is there a generic pattern in jq to flatten a 1-level nested array like this?

support

Most helpful comment

After fixing up the JSON:

jq '. as $in | .history[] | . as $h |  [$in.id, $in.date] + [$h[]]' 646.json
[
  1,
  "2014-12-30",
  1,
  "2014-12-30",
  "open"
]
[
  1,
  "2014-12-30",
  2,
  "2014-12-31",
  "close"
]

And thus:

jq -r '. as $in | .history[] | . as $h |  [$in.id, $in.date] + [$h[]]|@csv' 646.json
1,"2014-12-30",1,"2014-12-30","open"
1,"2014-12-30",2,"2014-12-31","close"

All 2 comments

After fixing up the JSON:

jq '. as $in | .history[] | . as $h |  [$in.id, $in.date] + [$h[]]' 646.json
[
  1,
  "2014-12-30",
  1,
  "2014-12-30",
  "open"
]
[
  1,
  "2014-12-30",
  2,
  "2014-12-31",
  "close"
]

And thus:

jq -r '. as $in | .history[] | . as $h |  [$in.id, $in.date] + [$h[]]|@csv' 646.json
1,"2014-12-30",1,"2014-12-30","open"
1,"2014-12-30",2,"2014-12-31","close"

That works perfect. Many thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ve3ied picture ve3ied  路  4Comments

tbelaire picture tbelaire  路  4Comments

neowulf picture neowulf  路  3Comments

rubensayshi picture rubensayshi  路  3Comments

thelonious picture thelonious  路  4Comments