How to parse JSON from stdin at Native Messaging host?
Asked Answered
I

1

0

Utilizing the code at How do I use a shell-script as Chrome Native Messaging host application as a template and given the file file.json which contains

{"text":"abc"}

following the code at Iterate over json with jq and the jq documentation

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

outputs

abc

Am not certain how to incorporate the pattern at 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)"')

into the template at the former Answer for the purpose of capturing the single property "text" (abc) from the JSON within the loop using jq for the ability to pass that text to another system call then printf the message to client.

What we are trying to achieve is

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

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

How to use jq within the code at the former Answer to get the JSON property as a variable?

Inge answered 22/1, 2018 at 15:32 Comment(0)
R
1

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

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

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

{
  "message": {
    "text": "abc"
  }
}
Rockies answered 22/1, 2018 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.