Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten array objects with parent values? #646

Closed
kmatt opened this issue Dec 11, 2014 · 3 comments
Closed

Flatten array objects with parent values? #646

kmatt opened this issue Dec 11, 2014 · 3 comments
Labels

Comments

@kmatt
Copy link

kmatt commented Dec 11, 2014

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?

@pkoppstein
Copy link
Contributor

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"

@kmatt
Copy link
Author

kmatt commented Dec 11, 2014

That works perfect. Many thanks.

@MostefaKamalLala
Copy link

7 years laters. I used it and still work with the actual jq. Thnx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants