在Shell脚本中遍历JSON数组 [英] Iterating through JSON array in Shell script

查看:3448
本文介绍了在Shell脚本中遍历JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在data.json文件中有一个如下的JSON数据

I have a JSON data as follows in data.json file

[
  {"original_name":"pdf_convert","changed_name":"pdf_convert_1"},
  {"original_name":"video_encode","changed_name":"video_encode_1"},
  {"original_name":"video_transcode","changed_name":"video_transcode_1"}
]

我想遍历数组并提取循环中每个元素的值.我看到了 jq .我发现很难使用它进行迭代.我该怎么办?

I want to iterate through the array and extract the value for each element in a loop. I saw jq. I find it difficult to use it to iterate. How can I do that?

推荐答案

只需使用将返回数组中每个项目的过滤器.然后循环搜索结果,只需确保使用紧凑的输出选项(-c),以便将每个结果放在一行上,并在循环中视为一项.

Just use a filter that would return each item in the array. Then loop over the results, just make sure you use the compact output option (-c) so each result is put on a single line and is treated as one item in the loop.

jq -c '.[]' input.json | while read i; do
    # do stuff with $i
done

这篇关于在Shell脚本中遍历JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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