如何在本机消息传递主机上从stdin解析JSON? [英] How to parse JSON from stdin at Native Messaging host?

查看:113
本文介绍了如何在本机消息传递主机上从stdin解析JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用如何使用shell脚本作为Chrome Native Messaging主机应用程序作为模板,并指定文件file.json其中包含

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

跟随使用jq遍历json jq文档

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

输出

abc

不确定如何在此答案

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)"')

进入先前答案中的模板,目的是使用jq从循环内的JSON捕获单个属性"text"(abc),以便能够将该文本传递给另一个系统调用,然后printf给客户的消息.

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.

我们想要实现的是

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

{"text":"abc"}从客户端(Chromium应用程序)发送到本机消息传递主机的位置.

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

如何在以前的答案的代码中使用jq来获取JSON属性作为变量?

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

推荐答案

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

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

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

如果再按echo "$json",结果将是:

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

这篇关于如何在本机消息传递主机上从stdin解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆