I'm working on a Bash script. In it, there are a couple occasions where I need to parse some JSON. My usual approach for that is as follows:
MY_JSON=$(some command that prints JSON to stdout)
RESULT=$(python -c "import json,sys;data=json.load(sys.stdin); python code here that prints out the value(s) I need")
This tends to work well. However, yesterday I ran into a problem. I had the following code:
MY_JSON=$(command that returns JSON containing an array of IDs)
IDS=$(echo "${MY_JSON}" | python -c "import json,sys;data=json.load(sys.stdin); for a in data['array']: print(a['id'])")
When I ran that code, I got "Syntax Error" with the caret pointing at the f in for.
In my googling, everything I found indicated that when you get a syntax error on the very first character of a statement, it usually means that you screwed something up in the previous statement. However, if I remove the for loop entirely, I get no syntax error. So, obviously, the problem is with the loop.
What did I do wrong? How can the syntax error be the first character of valid keyword?
I ended up finding the answer, which I'll post below to help others who are trying to build Python one-liners involving a for loop -- but I'm hoping someone can chime in with a better answer, perhaps using comprehensions (which I don't fully understand) or something else instead of the for loop so that I can actually accomplish this in a single line. Using a language other than Python would also be acceptable, so long as it's something typically available on a Linux host.
To be clear, I'd be looking for solutions using true JSON parsing, not some approximation using your favorite string manipulation tool (sed, awk, etc) that would be fragile with respect to things like whether the JSON is pretty-printed.