How to parse JSON from stdin at Native Messaging host?

ぃ、小莉子 提交于 2019-12-29 09:47:13

问题


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?


回答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"
  }
}


来源:https://stackoverflow.com/questions/48385086/how-to-parse-json-from-stdin-at-native-messaging-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!