从 JSON 行中提取片段

从 JSON 行中提取片段

我有许多相当长的 JSON 行 (~1.5KB),可能类似于: "XYZ":{"myString":"myValue",ABCD}

我只想从整行中提取“myString”:“myValue”。 grep 本身并不能帮助我,因为它都在一行上,我想将grepto的输出通过管道传输awk,但分隔符似乎无法按需要工作。有人可以帮忙吗?

请注意,该文档不仅仅是 JSON 文档。文件中有很多内容,其中一部分是 JSON 行,我想从中提取 myValue (作为 myString 的一部分)。一个示例可能如下所示:

    ....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...

答案1

myString假设该字符串位于文件中并构成完整且格式良好的 JSON 文档,以下命令将提取与文档中的任何键对应的值:

jq -r '..|select(type == "object" and has("myString")).myString' file.json

jq表达式递归地选择文档中具有myString键的 JSON 对象,然后提取该键的值。

相关内容