美文网首页
mac shell 修改json文件

mac shell 修改json文件

作者: Alex0825 | 来源:发表于2020-03-13 15:44 被阅读0次

1.使用 jq

brew install jq

2.json文件如下

{
  "plist_url": "xxx",
  "apk_url": "xxx",
  "title": "xxx"
}

3.遍历脚本

title=app下载
project_pub_path=app/main_71
plist_url=https://xxx.com/files/$project_pub_path/app.plist
apk_url=https://xxx.com/files/$project_pub_path/bhfae.apk

cat h5_path/config.json | 
    jq 'to_entries | 
        map(if .key == "plist_url"
            then . + {"value":"'${plist_url}'"}
            elif .key == "apk_url"
            then . + {"value":"'${apk_url}'"}
            elif .key == "title"
            then . + {"value":"'${title}'"}
            else .
            end
            ) | 
            from_entries' >tmp.json

mv tmp.json h5_path/config.json

输出

{
  "plist_url": "https://xxx.com/files/app/main_71/app.plist",
  "apk_url": "https://xxx.com/files/app/main_71/bhfae.apk",
  "title": "app下载"
}

用到了 jq 的 if elif

jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
Input 2
Output "many"

引用 jq手册

相关文章

网友评论

      本文标题:mac shell 修改json文件

      本文链接:https://www.haomeiwen.com/subject/dqllshtx.html