无法在Unix shell中使用JQ迭代JSON响应对象

时间:2015-01-06 14:25:03

标签: json loops unix jq

我试图在UNIX中迭代一个JSON对象,其中的想法是获取不同的值并将其附加到字符串并将其转发为syslog。以下是代码。

//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)"

我收到内容[$ 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": {

1 个答案:

答案 0 :(得分:4)

好的,我得到了答案。

我们可以使用以下语法使for for循环。

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