Flatten array and reformat the JSON using jq

Viewed 60

I'm trying to accomplish two things:

  • Flatten the array in this json
  • Reformat the json to a simple payload

Here's is the json I'm working with:

{
  "id": 1,
  "feed": "jqQuestion",
  "organization": "question.com",
  "category": "question",
  "contact": {
    "name": "",
    "email": "",
    "phone": ""
  },
  "website": "https://thankyou.com/",
  "schedule": "daily",
  "stamp_added": "2017-09-27 00:00:00",
  "stamp_updated": "2022-09-10 15:51:09",
  "stamp_pulled": "2022-09-10 08:51:03",
  "stamp_modified": "2019-08-07 18:29:00",
  "nextarray": {
    "value": 1,
    "value2": 2
  }
}

I'm hoping to remove the "title" of the arrays, just keep the content, and align the contents of the array with the reset of the json:

{
  "id": 1,
  "feed": "jqQuestion",
  "organization": "question.com",
  "category": "question",
  "name": "",
  "email": "",
  "phone": ""
  "website": "https://thankyou.com/",
  "schedule": "daily",
  "stamp_added": "2017-09-27 00:00:00",
  "stamp_updated": "2022-09-10 15:51:09",
  "stamp_pulled": "2022-09-10 08:51:03",
  "stamp_modified": "2019-08-07 18:29:00",
  "value": 1,
  "value2": 2
}

I've tried a few approaches, using sed, awk, and jq. I think using jq would be "optimal", but haven't been able to get the format right with the mentioned approaches. Any guidance here would be much appreciated!

2 Answers

If you know that contact and nextarray are the sub-objects to merge, then:

jq '. + .contact + .nextarray | del(.contact) | del(.nextarray)' file.json
{
  "id": 1,
  "feed": "jqQuestion",
  "organization": "question.com",
  "category": "question",
  "website": "https://thankyou.com/",
  "schedule": "daily",
  "stamp_added": "2017-09-27 00:00:00",
  "stamp_updated": "2022-09-10 15:51:09",
  "stamp_pulled": "2022-09-10 08:51:03",
  "stamp_modified": "2019-08-07 18:29:00",
  "name": "",
  "email": "",
  "phone": "",
  "value": 1,
  "value2": 2
}

The + operator, when given object operands, will merge the objects.

Generically:

jq '
    to_entries 
    | map(if (.value|type) == "object" then .value else {(.key): .value} end) 
    | add
'
{
  "id": 1,
  "feed": "jqQuestion",
  "organization": "question.com",
  "category": "question",
  "name": "",
  "email": "",
  "phone": "",
  "website": "https://thankyou.com/",
  "schedule": "daily",
  "stamp_added": "2017-09-27 00:00:00",
  "stamp_updated": "2022-09-10 15:51:09",
  "stamp_pulled": "2022-09-10 08:51:03",
  "stamp_modified": "2019-08-07 18:29:00",
  "value": 1,
  "value2": 2
}
jq -c 'to_entries[]|flatten[]'

would already get you to

"id"
1
"feed"
"jqQuestion"
"organization"
"question.com"
"category"
"question"
"contact"
{"name":"","email":"","phone":""}
"website"
"https://thankyou.com/"
"schedule"
"daily"
"stamp_added"
"2017-09-27 00:00:00"
"stamp_updated"
"2022-09-10 15:51:09"
"stamp_pulled"
"2022-09-10 08:51:03"
"stamp_modified"
"2019-08-07 18:29:00"
"nextarray"
{"value":1,"value2":2}

to create ur desired output should be trivial from here

Related