2

使用jq组装JSON

 2 years ago
source link: https://www.lujun9972.win/blog/2018/03/01/%E4%BD%BF%E7%94%A8jq%E7%BB%84%E8%A3%85json/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用jq组装JSON

要在shell下手工拼装JSON是一件非常痛苦的事情。

偶然发现原来 jq 不仅能够解析JSON,还能拼装JSON。

只要使用 jq--arg jq参数名 jq参数值 参数就能将shell变量的值传递,然后直接在 jq 表达式中写出要拼装JSON的结构就行了,类似这样:

title="TEST OF JQ"
source="JQ NB"
echo '{}'|jq '{"title":$title,"content":$source}' --arg title "$title" --arg source "$source"

就能得到这样的结果了:

{
  "title": "TEST OF JQ",
  "content": "JQ NB"
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK