Home > Mobile >  Merge and Reformat JSON files using jq
Merge and Reformat JSON files using jq

Time:07-28

I have two different JSON files returned with Python code for Twitter data. The first JSON file is in the form below:

A.json

{"tweet_id": "1212024242595926028", "username": "THPDPIO", "created_at": "2019-12- 
31T14:54:32.000Z", "tweets": {"0": "Folks, it\u2019s simple!! You know what to do and 
what not to do...don\u2019t drink and drive and you don\u2019t have to worry about 
ANY consequences.  Btw...jail is just a small part of it...think about the 
possibility killing someone or killing yourself...again it\u2019s simple... 
\ud83d\ude42 #stayhome"}}
{"tweet_id": "1212024242595926028", "username": "TheAliciaRanae", "created_at": 
"2019-12-31T15:11:51.000Z", "tweets": {"1": "@THPDPIO Stay home and drink and pass 
out and leave everyone else alone lol that\u2019s what I\u2019ll be doing lol HAPPY 
NEW YEAR!"}}
{"tweet_id": "1212024242595926028", "username": "duane4343", "created_at": "2019-12- 
31T15:21:37.000Z", "tweets": {"1": "@THPDPIO Happy New Year"}}
{"tweet_id": "1212024242595926028", "username": "HollyBr34731868", "created_at": 
"2019-12-31T15:24:25.000Z", "tweets": {"1": "@THPDPIO Hope everyone has a safe 
night."}}

{"tweet_id": "1211503874395254785", "username": "UNDPoliceDept", "created_at": "2019- 
12-30T04:26:46.000Z", "tweets": {"0": "Typical North Dakotan.... #BestCopsAround 
#NoTravelAdvised #StayHome"}}
{"tweet_id": "1211503874395254785", "username": "UNDPoliceDept", "created_at": "2019- 
12-30T04:27:40.000Z", "tweets": {"1": "@NDHighwayPatrol"}}
{"tweet_id": "1211503874395254785", "username": "BorgenEthan", "created_at": "2019- 
12-30T05:28:48.000Z", "tweets": {"1": "@UNDPoliceDept Nah i definitely look like the 
first one"}}

With jq, i wrote some commands to choose the fields I want {NB: This was done in https://jqplay.org} with the command {tweet_id: .tweet_id, username: .username, reply: .tweets} | group_by(.tweet_id) but I get the error that

q: error (at :1): Cannot index string with string "tweet_id"

My desired output is to get the sample below for the first file:

{
"tweet_id": "1212024242595926028",
"username": "THPDPIO",
"reply": {
  "0": "Folks, it’s simple!! You know what to do and what not to do...don’t drink and drive and you don’t have to worry about ANY consequences.  Btw...jail is just a small part of it...think about the possibility killing someone or killing yourself...again it’s simple...            
  • Related