How to parse JSON from stdin on a Native Messaging host?

Using code in How to use a shell script as a host host application for Chrome as a template and a given file.json file that contains

 {"text":"abc"} 

following the iteration code over json with jq and jq documentation

 $ cat file.json | jq --raw-output '.text' 

exits

 abc 

Not sure how to include a template in this Answer

 while read -r id name date; do echo "Do whatever with ${id} ${name} ${date}" done< <(api-producing-json | jq --raw-output '.newList[] | "\(.id) \(.name) \(.create.date)"') 

to the template in the first answer in order to capture the only "text" ( abc ) property from JSON in a loop using jq to be able to pass this text to another system call printf message to the client.

What we are trying to achieve

 json=$(<bash program> <captured JSON property>) message='{"message": "'$json'"}' 

where {"text":"abc"} sent to the Native Messaging host from the client (Chromium application).

How to use jq in code in the previous Answer to get the JSON property as a variable?

0
source share
1 answer

Assuming file.json contains JSON as indicated, I believe that all you need is:

 json=$(jq '{message: .}' file.json) 

If you then echo "$json" , the result will be:

 { "message": { "text": "abc" } } 
+1
source

Source: https://habr.com/ru/post/1275058/


All Articles