gpt4 book ai didi

json - 无法在 Unix shell 中使用 JQ 遍历 JSON 响应对象

转载 作者:行者123 更新时间:2023-12-01 12:38:01 25 4
gpt4 key购买 nike

我正在尝试遍历 UNIX 中的 JSON 对象,其想法是获取不同的值并将其附加到字符串并将其作为系统日志转发。下面是代码。

//picking up the length of Object
count=$(jq '.content | length' red)
#echo $count

enter code here

for((i=0;i<$count;i++))
do
echo "MY VALUE OF I"
echo $i
//THE BELOW LINE GIVES ERROR UPON USAGE of $i
id="$(cat red | jq '.content[$i].id')"
source=$(cat red | jq '.content[$i].entitySummary.source')
.
.

#syslogString="ID=$id SOURCE=$source SUMMARY=$summaryText TITLE=$title DESCRIPTION=$description SEVERITY=$severity MITIGATION=$mitigation IMPACT=$impactDescrip$

echo $id
echo "value of ID ($id)"

我收到 content[$i] 的编译错误并且无法获得相同的解决方法。

响应类如下所示:

Page {
content ( array[ ClientIncident ] )
The list of results that make up the page. The number of elements should be less than or equal to the currentPage size.
currentPage ( Pagination )
Size and offset information about the current page.
total ( integer )
The total number of results found. If there are a large number of results, this may be an estimate. Accuracy should improve as the page approaches the end of the resultset.
}

在内容下,JSON 响应如下所示:

{
"content": [
{
"id": 951653,
"version": 12,
"score": 100,
"entitySummary": {
"source": "somewebsite",
"summaryText": "someTEXT here",
"domain": "www.domian.com",
"sourceDate": "2014-12-19T17:00:00.000Z",
"type": "WEB_PAGE"
},
"type": "SomeTYPE",
"title": "some Title",
"description": "some description ",
"occurred": "2014-12-19T17:00:00.000Z",
"verified": "2014-12-19T17:17:22.326Z",
"tags": [
{
"id": 424,
"name": "Data Breach or Compromise",
"type": "IMPACT_EFFECTS"
},
{
"id": 1064,
"name": "United States",
"type": "TARGET_GEOGRAPHY"
},
],
"severity": "MEDIUM",
"clientId": "NET",
"alerted": "2014-12-19T17:39:55.500Z",
"mitigation": "MititgationINFO",
"impactDescription": "IMpact description": 0
},
{
"id": 951174,
"version": 8,
"score": 100,
"entitySummary": {

最佳答案

好的,我得到了这个问题的答案。

我们可以使用下面的语法使 is 在 for 循环中工作。

id=$(cat red | jq '.content['${i}'].id')

关于json - 无法在 Unix shell 中使用 JQ 遍历 JSON 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com