如何使用 Jq linux 将 json 数据拼接成单行
How to Stitch json data into single line with Jq linux
我有一个 json 流,我想用 jq ( Linux )
将输出变成单行
由此
{
"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
}
对此
{"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},
希望有人能给出解决方案。
--compact-output
(或-c
)选项将在一行中打印每个对象,而--join-output
(或-j
)选项将省略换行符对象之间。
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}
请注意,虽然这是有效的 JSON,但您想要的输出不是。它在对象之后有逗号。如果您真的需要它们,您可以使用 --raw-output
(或 -r
)选项打印逗号之类的文字字符串:
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},
我有一个 json 流,我想用 jq ( Linux )
将输出变成单行由此
{
"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
}
对此
{"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},
希望有人能给出解决方案。
--compact-output
(或-c
)选项将在一行中打印每个对象,而--join-output
(或-j
)选项将省略换行符对象之间。
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}
请注意,虽然这是有效的 JSON,但您想要的输出不是。它在对象之后有逗号。如果您真的需要它们,您可以使用 --raw-output
(或 -r
)选项打印逗号之类的文字字符串:
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},