0

I have a shell script in which i get a jsonresponse. I am using python to parse the json , and inside each json object i want to fetch id and again submit a curl request for each application Id from a shell script.

Currently am doing it like this , which am unable to do it successfully due to syntax errors. Can you please help me out

Any suggestions are helpful thank you.

curl -v -X GET -H "Content-Type: application/json" 'http://host/ws/v1/cluster/apps?state=PENDING' |
    python3 -c "import sys, json; jsonObject = json.load(sys.stdin)['apps']; for row in jsonObject['app']: print(row['id'])"

Output:

{"apps":{"app":[{"id":1},{"id":2}]}}

Mozhi
  • 499
  • 6
  • 15
  • Add output of curl, and how do you want to use those *ids* to your question. – oguz ismail Apr 08 '19 at 07:22
  • Why not do everything in Python, using the `requests` module? – Barmar Apr 08 '19 at 07:22
  • Or use `jq` to parse the returned value in `bash`. – Barmar Apr 08 '19 at 07:23
  • Why are you sending the `Content-type` header in a `GET` request? They have no contents. – Barmar Apr 08 '19 at 07:24
  • @Barmar content-type not required as you mentioned . I have a requirement to do it in bash itself, can't allow the control flow outside of bash – Mozhi Apr 08 '19 at 07:38
  • @oguzismail Added output section , i want to extract the id's (values) and do another curl on each id . – Mozhi Apr 08 '19 at 07:42
  • @Barmar: "[GET requests] have no contents." [Not correct](https://stackoverflow.com/questions/978061/http-get-with-request-body). They *shouldn't*, but can, and sometimes do. – Amadan Apr 08 '19 at 07:50
  • 1
    `curl -sS ... | jq -r '.apps.app[].id'` extracts all ids, you can assign them to bash variables and use for other requests. but it might not be necessary, a curl config can be created using jq and directly piped to `curl -sSK-`. – oguz ismail Apr 08 '19 at 07:52
  • @Amadan "it is never useful to do so." Since it has no semantic meaning, specifying the type is also unnecessary. – Barmar Apr 08 '19 at 14:09
  • @Barmar: 1) Read the rest of that answer, in particular the update that says with the updated RFC, the restriction that GET body "SHOULD be ignored" semantics is gone, replaced with a weaker claim that GET body does not have a defined semantics by the RFC, but is not prohibited. 2) SHOULD is not MUST, it is allowed to be broken; 3) Read the comments about Elasticsearch on the same page; whether you think it's a good idea or not (I think not, FTR), people are actually doing it, and if you need to interact with such a product you may be forced to, so "never useful" is disingenuous. – Amadan Apr 08 '19 at 14:24
  • @Barmar: Trying to get back to topic, `Content-Type` is truly unnecessary _here_, since _this_ request has no body. – Amadan Apr 08 '19 at 14:26

0 Answers0