如何从Native Messaging主机的stdin解析JSON?

时间:2018-01-22 15:32:47

标签: json bash stdin jq chrome-native-messaging

利用How do I use a shell-script as Chrome Native Messaging host application处的代码作为模板,并提供包含

的文件file.json
{"text":"abc"}

关注Iterate over json with jqjq documentation

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

输出

abc

我不确定如何在此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)"')

进入前一个模板中的模板,目的是使用"text"abc中的JSON获取单个属性jqprintf)将该文本传递给另一个系统调用,然后将json=$(<bash program> <captured JSON property>) message='{"message": "'$json'"}' 消息传递给客户端。

我们想要实现的是

{"text":"abc"}

从客户端(Chromium app)将jq发送到Native Messaging主机。

如何在前Answer的代码中使用JSONMobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); 属性作为变量?

1 个答案:

答案 0 :(得分:1)

假设file.json包含指示的JSON,我相信您需要的只是:

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

如果您然后echo "$json",结果将是:

{
  "message": {
    "text": "abc"
  }
}