extrai parte da linha JSON

1

Eu tenho várias linhas JSON que são bem longas (~ 1.5KB) e podem ser parecidas com: "XYZ":{"myString":"myValue",ABCD}

e eu só quero extrair "myString": "myValue" da linha inteira. grep sozinho não me ajuda, já que está tudo em uma linha, pensei em canalizar a saída de grep para awk , mas o divisor não parece funcionar conforme necessário. Alguém pode ajudar aqui?

Mente, o documnent não é um documento somente JSON. Há muito conteúdo no arquivo e parte dele são linhas JSON das quais eu gostaria de extrair o myValue (como parte do myString). Um exemplo pode parecer:

    ....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...
    
por cerr 31.05.2018 / 06:33

1 resposta

3

Supondo que a string esteja em um arquivo e constitua um documento JSON completo e bem formado, o comando a seguir extrairia o valor correspondente a qualquer chave myString no documento:

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

A expressão jq seleciona recursivamente objetos JSON no documento que possui uma chave myString e, em seguida, extrai o valor dessa chave.

    
por 31.05.2018 / 08:28