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
    },
7
  • That's not valid JSON -- some of its quotes are instead of ", and others are unbalanced. May 18, 2020 at 17:57
  • 1
    That said, we do already have Q&A entries telling how to do this with bash and jq. May 18, 2020 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) May 18, 2020 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, 2020 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. May 18, 2020 at 19:07

0

Browse other questions tagged or ask your own question.