0

am trying to write a shell script where I need to read a json data, and according to the status = 'SUCCESS', I need to fetch the id of that array. So basically i need to loop through the list of array, and check in each whether status ='SUCCESS', if true then get the id and break

sample json:

[
    {
        "id": “id_1",
        "name": "INSIGHTS_METRICS",
        "type": "INSIGHTS_METRICS",
        "startTime": 1589354439897,
        "endTime": 0,
        "timeTaken": 0,
        "inputFiles": null,
        "outputFiles": null,
        "status": “FAIL",
        "statusMessage": null,
        "subCode": null,
        "location": null,
        "externalId": null,
        "parentId": null,
        "childJobs": null
    },
    {
        "id": “id_2",
        "name": "INSIGHTS_METRICS",
        "type": "INSIGHTS_METRICS",
        "startTime": 1589348936383,
        "endTime": 0,
        "timeTaken": 0,
        "inputFiles": null,
        "outputFiles": null,
        "status": "RUNNING",
        "statusMessage": null,
        "subCode": null,
        "location": null,
        "externalId": null,
        "parentId": null,
        "childJobs": null
    },
    {
        "id": "id_3",
        "name": "INSIGHTS_METRICS",
        "type": "INSIGHTS_METRICS",
        "startTime": 1589348677458,
        "endTime": 0,
        "timeTaken": 0,
        "inputFiles": null,
        "outputFiles": null,
        "status": “SUCCESS",
        "statusMessage": null,
        "subCode": null,
        "location": null,
        "externalId": null,
        "parentId": null,
        "childJobs": null
    },
anurag1007
  • 107
  • 1
  • 10
  • That's not valid JSON -- some of its quotes are `“` instead of `"`, and others are unbalanced. – Charles Duffy May 18 '20 at 17:57
  • 1
    That said, we do already have Q&A entries telling how to do this with bash and `jq`. – Charles Duffy May 18 '20 at 17:57
  • Also -- `bash` and `sh` are two different shells. Tag for one or the other, but not both. (When you run `bash` under the name `sh`, it shuts off some of its functionality to be closer to the behaviors specified in the POSIX sh standard; where `sh` can also be `ash`, `dash`, or another non-bash shell entirely) – Charles Duffy May 18 '20 at 17:58
  • @CharlesDuffy i have modified the json, basically i want to loop through the array of obj {assigned to a variable}, and put a condition to check the value of status =Success and if true break, and return the id. Its pretty stratigh forward use case, its just am new to shell script so struggling. am using jq, but how to loop through, i need help in that. Didnot find much results for tha – anurag1007 May 18 '20 at 18:24
  • jq itself can loop. `jq '.[]'` loops through all the things in the top-level list. `jq '.[] | select(.status == "SUCCESS") | .id` loops through only the things with `SUCCESS` as the status, and then writes their IDs. Add `-r` and then you write those IDs unquoted ("raw"). At that point the only problem you have left (how to loop through that output in your shell) is covered in detail in [BashFAQ #1](http://mywiki.wooledge.org/BashFAQ/001). – Charles Duffy May 18 '20 at 19:07
  • I dont think this is duplicate, but will help other. got a solution what I was looking for: curl -s "https://jobs.github.com/positions.json?description=java" | jq '.[] | select(.type=="Full Time")' – anurag1007 May 19 '20 at 08:34
  • We have _lots_ of Q&A entries already in the knowledgebase describing that technique, the linked duplicate among them. Being answered by teaching the same solution or technique is exactly what _makes_ two questions duplicative. – Charles Duffy May 19 '20 at 14:11

0 Answers0