Thursday, April 7, 2022

[SOLVED] How to Stitch json data into single line with Jq linux

Issue

I have a json stream that I want to make the output into single line with jq ( Linux )

From this
{
  "Icao": "800B0A",
  "Alt": 15375,
  "GAlt": 15278,
  "AltT": 0,
  "Lat": 10.579605,
  "Long": 76.506561,
  "Mlat": false,
  "Spd": 334.5,
  "Trak": 26.3,
  "Vsi": 832
}
{
  "Icao": "800463",
  "Alt": 3575,
  "GAlt": 3445,
  "AltT": 0,
  "Lat": 40.626663,
  "Long": -74.261963,
  "Mlat": false,
  "Spd": 274.4,
  "Trak": 316.2,
  "Vsi": 1472
}
To this
{"Icao":"A379CD","Alt":32025,"GAlt":32114,"InHg":30.0088577,"AltT":0,"Lat":38.335918,"Long":-104.720284,"Mlat":false,"Spd":403.0,"Trak":240.0,"Vsi":64},{"Icao":"345042","Alt":22850,"GAlt":22782,"InHg":29.8523617,"AltT":0,"Lat":39.655243,"Long":-8.845612,"Mlat":false,"Spd":308.5,"Trak":212.6,"Vsi":-1216},{"Icao":"40753D","Alt":37000,"GAlt":37050,"InHg":29.9704723,"AltT":0,"Lat":41.940674,"Long":-8.029816,"Mlat":false,"Spd":468.4,"Trak":190.2,"Vsi":-64},

Hope someone could hand a solution for the problem.


Solution

The --compact-output (or -c) option qill print each object in a single line, and the --join-output (or -j) option will omit the newline character between the objects.

jq -cj .
{"Icao":"800B0A","Alt":15375,"GAlt":15278,"AltT":0,"Lat":10.579605,"Long":76.506561,"Mlat":false,"Spd":334.5,"Trak":26.3,"Vsi":832}{"Icao":"800463","Alt":3575,"GAlt":3445,"AltT":0,"Lat":40.626663,"Long":-74.261963,"Mlat":false,"Spd":274.4,"Trak":316.2,"Vsi":1472}

Note that while this is valid JSON, your desired output is not. It has commas after the objects. If you really need them, you can print a literal string like a comma using the --raw-output (or -r) option:

jq -cjr '.,","'
{"Icao":"800B0A","Alt":15375,"GAlt":15278,"AltT":0,"Lat":10.579605,"Long":76.506561,"Mlat":false,"Spd":334.5,"Trak":26.3,"Vsi":832},{"Icao":"800463","Alt":3575,"GAlt":3445,"AltT":0,"Lat":40.626663,"Long":-74.261963,"Mlat":false,"Spd":274.4,"Trak":316.2,"Vsi":1472},


Answered By - pmf
Answer Checked By - Willingham (WPSolving Volunteer)