
Ich habe eine Reihe ziemlich langer JSON-Zeilen (~1,5 KB) und die ungefähr so aussehen könnten:
"XYZ":{"myString":"myValue",ABCD}
und ich möchte nur „myString“: „myValue“ aus der gesamten Zeile extrahieren. grep allein hilft mir nicht, da alles in einer Zeile steht. Ich habe überlegt, die Ausgabe weiterzuleiten, grep
aber awk
der Teiler scheint nicht wie gewünscht zu funktionieren. Kann hier jemand helfen?
Beachten Sie, dass das Dokument kein reines JSON-Dokument ist. Die Datei enthält viel Inhalt und ein Teil davon sind JSON-Zeilen, aus denen ich myValue (als Teil von myString) extrahieren möchte. Ein Beispielteil könnte so aussehen:
....other content...
....other content...
....other content...
....other content...!: {"response":{"container":{"id":"655ccc89-915c-4aae-a927-cca34bbedc29","timestamp":"2018-May-30
21:05:29.789543"},"id":"00000002-0000-0000-0000-000000000003"},"frames":{"frame":
{"id":"585","timestamp":"2016-Nov-30 12:57:06.500000","lps":{"lp":{"myString":"myValue","text":"8578D","wtext":"8578D","confidence":"4","bkcolor":"16777215","color":"16777215","type":"550171","ntip":"6","cct_country_short":"USA","cct_state_short":"FL","tips":{"tip":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"3"},"tip":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"4"},"tip":{"poly":{"p":{"x":"450","y":"28"},"p":{"x":"468","y":"28"},"p":{"x":"467","y":"63"},"p":{"x":"449","y":"63"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"98"},"tip":{"poly":{"p":{"x":"471","y":"28"},"p":{"x":"489","y":"28"},"p":{"x":"488","y":"63"},"p":{"x":"470","y":"63"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"tip":{"poly":{"p":{"x":"495","y":"28"},"p":{"x":"512","y":"28"},"p":{"x":"511","y":"63"},"p":{"x":"494","y":"63"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"98"},"tip":{"poly":{"p":{"x":"516","y":"28"},"p":{"x":"534","y":"28"},"p":{"x":"533","y":"63"},"p":{"x":"515","y":"63"}},"bkcolor":"16777215","color":"0","code":"68","code_ascii":"D","confidence":"98"}},"ncharacter":"5","characters":{"characater":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"4"},"characater":{"poly":{"p":{"x":"450","y":"28"},"p":{"x":"468","y":"28"},"p":{"x":"467","y":"63"},"p":{"x":"449","y":"63"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"98"},"characater":{"poly":{"p":{"x":"471","y":"28"},"p":{"x":"489","y":"28"},"p":{"x":"488","y":"63"},"p":{"x":"470","y":"63"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"characater":{"poly":{"p":{"x":"495","y":"28"},"p":{"x":"512","y":"28"},"p":{"x":"511","y":"63"},"p":{"x":"494","y":"63"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"98"},"characater":{"poly":{"p":{"x":"516","y":"28"},"p":{"x":"534","y":"28"},"p":{"x":"533","y":"63"},"p":{"x":"515","y":"63"}},"bkcolor":"16777215","color":"0","code":"68","code_ascii":"D","confidence":"98"}},"det_time_us":"230387","poly":{"p":{"x":"353","y":"8"},"p":{"x":"541","y":"8"},"p":{"x":"540","y":"77"},"p":{"x":"352","y":"78"}}},"lp":{"licenseplate":"FRJ724","text":"FRJ724","wtext":"FRJ724","confidence":"66","bkcolor":"16777215","color":"16777215","type":"540122","ntip":"6","cct_country_short":"USA","cct_state_short":"NY","tips":{"tip":{"poly":{"p":{"x":"1553","y":"247"},"p":{"x":"1560","y":"247"},"p":{"x":"1560","y":"267"},"p":{"x":"1553","y":"267"}},"bkcolor":"16777215","color":"0","code":"70","code_ascii":"F","confidence":"94"},"tip":{"poly":{"p":{"x":"1561","y":"248"},"p":{"x":"1568","y":"248"},"p":{"x":"1568","y":"267"},"p":{"x":"1561","y":"267"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"99"},"tip":{"poly":{"p":{"x":"1570","y":"248"},"p":{"x":"1575","y":"248"},"p":{"x":"1575","y":"267"},"p":{"x":"1570","y":"267"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"84"},"tip":{"poly":{"p":{"x":"1586","y":"247"},"p":{"x":"1592","y":"247"},"p":{"x":"1592","y":"267"},"p":{"x":"1586","y":"267"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"tip":{"poly":{"p":{"x":"1593","y":"248"},"p":{"x":"1600","y":"248"},"p":{"x":"1600","y":"267"},"p":{"x":"1593","y":"267"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"90"},"tip":{"
poly":{"p":{"x":"1602","y":"248"},"p":{"x":"1607","y":"248"},"p":{"x":"1607","y":"266"},"p":{"x":"1602","y":"266"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"95"}},"ncharacter":"6","characters":{"characater":{"poly":{"p":{"x":"1553","y":"247"},"p":{"x":"1560","y":"247"},"p":{"x":"1560","y":"267"},"p":{"x":"1553","y":"267"}},"bkcolor":"16777215","color":"0","code":"70","code_ascii":"F","confidence":"94"},"characater":{"poly":{"p":{"x":"1561","y":"248"},"p":{"x":"1568","y":"248"},"p":{"x":"1568","y":"267"},"p":{"x":"1561","y":"267"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"99"},"characater":{"poly":{"p":{"x":"1570","y":"248"},"p":{"x":"1575","y":"248"},"p":{"x":"1575","y":"267"},"p":{"x":"1570","y":"267"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"84"},"characater":{"poly":{"p":{"x":"1586","y":"247"},"p":{"x":"1592","y":"247"},"p":{"x":"1592","y":"267"},"p":{"x":"1586","y":"267"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"characater":{"poly":{"p":{"x":"1593","y":"248"},"p":{"x":"1600","y":"248"},"p":{"x":"1600","y":"267"},"p":{"x":"1593","y":"267"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"90"},"characater":{"poly":{"p":{"x":"1602","y":"248"},"p":{"x":"1607","y":"248"},"p":{"x":"1607","y":"266"},"p":{"x":"1602","y":"266"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"95"}},"det_time_us":"672351","poly":{"p":{"x":"1543","y":"236"},"p":{"x":"1618","y":"237"},"p":{"x":"1618","y":"275"},"p":{"x":"1543","y":"274"}}}},"det_time_us":"1703430"}}}
....other content...
....other content...
....other content...
Antwort1
Vorausgesetzt, der String befindet sich in einer Datei und stellt ein vollständiges und wohlgeformtes JSON-Dokument dar, würde der folgende Befehl den Wert extrahieren, der jedem Schlüssel myString
im Dokument entspricht:
jq -r '..|select(type == "object" and has("myString")).myString' file.json
Der jq
Ausdruck wählt rekursiv JSON-Objekte im Dokument aus, die über einen myString
Schlüssel verfügen, und extrahiert dann den Wert dieses Schlüssels.