json - 使 JSON 输出可 grep 吗?

标签 json linux bash awk grep

<分区>

我的 JSON 输出如下所示;

{  
   "1":{  
      "state":{  
         "on":false,
         "bri":124,
         "hue":14985,
         "sat":252,
         "effect":"none",
         "xy":[  
            0.5182,
            0.4363
         ],
         "ct":480,
         "alert":"none",
         "colormode":"xy",
         "reachable":true
      },
      "type":"Extended color light",
      "name":"Slaapkamer rechts",
      "modelid":"LCT001",
      "manufacturername":"Philips",
      "uniqueid":"00:17:88:01:00:dc:36:68-0b",
      "swversion":"66013187"
   },
   "2":{  
      "state":{  
         "on":false,
         "bri":125,
         "hue":14984,
         "sat":252,
         "effect":"none",
         "xy":[  
            0.5182,
            0.4363
         ],
         "ct":480,
         "alert":"none",
         "colormode":"xy",
         "reachable":true
      },
      "type":"Extended color light",
      "name":"Slaapkamer links",
      "modelid":"LCT001",
      "manufacturername":"Philips",
      "uniqueid":"00:17:88:01:00:dc:33:99-0b",
      "swversion":"66013187"
   }
}

有谁知道使此输出可 grep 的简单方法?或者任何其他方式将输出放在一行上,以便我可以在 json 输出上使用 grep、awk 等工具? 如果我现在对“色调”一词执行 grep,那么我会得到 2 行;

"hue":14985,
"hue":14984,

虽然我有时只想在第一个或第二个上 grep..

有什么想法吗?

最佳答案

使用jq:

jq '.["1"].state.hue' file

输出:

14985

It is not possible to use '.1.state.hue' because numbers are a special case.


jq '.["2"].state.hue' file

输出:

14984

jq '.[].state.hue' file

输出:

14984
14985

关于json - 使 JSON 输出可 grep 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483221/

相关文章:

bash - 如何使用printf打印 "-"

c# - HTML &lt;textarea&gt; 仅将 '\n' 发送回服务器

python - 在Python中使用argparse解析整个JSON

java - 将多个属性的 Jackson 自定义反序列化器转换为值对象类

linux - 用于从 shell 启动的命令的标准输入/标准输出

linux - 如何查找特定文件在给定时间范围内被修改的次数

json - 当 HttpClient 映射不会时,如何正确解析 TypeScript 类的 Date 对象?

linux - 我的 Bacula 传输率非常低。我如何配置导演来改善这一点?

linux - 从虚拟盒子中删除已安装的linux系统

python - 如何在 python 中使用来自 bash 的数据流和子进程