0

I'm trying to write a shell script where I parse some JSON. Specifically, I want to extract the value of an "id" element. I have the below (I have Python 3 installed on my system) ...

user_json=$(curl --header "Content-type: application/json" -H "Authorization: Bearer $json_token" --data "$req" --request POST "$URL/mypath")
echo $user_json
user_id=$(echo $user_json | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["id"])')
echo $user_id

However the above results in a

/opt/scripts/sync_script.sh: line 36: /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python: cannot execute binary file

on the "python" line (the $user_id var is never printed). What else do I need to do to get this working? When the $user_json is printed out, I can clearly see the "id" value ...

{"id":"4b8d53a5-d0b5-4175-82ec-8aeda435ba62","first_name":"ddd"}

Edit: In response to comments, I get the below error when attempting to see the version of my Python. Permissions are included as well.

localhost:tmp davea$ /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python -version
-bash: /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python: cannot execute binary file
localhost:tmp davea$ ls -al /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python
-rwxr-xr-x 1 davea admin 2382880 Apr 16  2019 /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python
Dave
  • 15,639
  • 133
  • 442
  • 830
  • Do you have permission to run `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/python`? – Poshi Mar 05 '20 at 16:06
  • This isn't really a question about parsing in any meaningful way -- if you can't run your Python interpreter at all, it won't run *at all*; that doesn't depend on what the specific code you're feeding it is. – Charles Duffy Mar 05 '20 at 16:09
  • @Poshi, this doesn't look like lack of permissions -- that has a different and distinct error. This looks like, maybe, a binary built for a different system architecture (running a 32-bit binary on an OS version that only supports 64-bit, f/e). – Charles Duffy Mar 05 '20 at 16:10
  • @Poshi, Interestingly, no, I'm not able to run the binary. I included what happens when I try and list the version as well as the permissions. – Dave Mar 05 '20 at 16:12
  • @CharlesDuffy, good point. But at the end, it boils down to get the interpreter running. The code is OK. Maybe using `file` on the interpreter can shed some light regarding the kind of binary OP have, so he can check if it is a valid one. – Poshi Mar 05 '20 at 16:15
  • The path to your Python interpreter looks wrong. It normally ends in `.../bin/python3` Where did you get that path? – Mark Setchell Mar 05 '20 at 16:21
  • If `file` says it's built for the right architecture, I'd then proceed to checking the shared libraries. But this really needs to be a question specifically about getting your Python interpreter (installed by whichever software maintains `/usr/local/Cellar`, on whichever specific OS have) running. – Charles Duffy Mar 05 '20 at 16:28
  • 1
    Why have you started another question.... let's resolve this one first!!! https://stackoverflow.com/q/60550018/2836621 – Mark Setchell Mar 05 '20 at 16:36
  • 1
    @MarkSetchell, *I* advised starting another question, because the underlying problem here is very different from the immediate problem, so an answer focused around parsing JSON is unlikely to help someone else with the "cannot execute binary" problem. – Charles Duffy Mar 05 '20 at 18:28

1 Answers1

1

Did you try using python -m|json.tool utility or jq[.] In bash i would simply do something like this,

curl --header "Content-type: application/json" -H "Authorization: Bearer $json_token" --data "$req" --request POST "$URL/mypath" | python -m json.tool

or

curl --header "Content-type: application/json" -H "Authorization: Bearer $json_token" --data "$req" --request POST "$URL/mypath" |jq [.]

And also make sure python is set properly in youe env $PATH

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441