1

i have a stupid error that I can't figure out how to resolve.

I need to execute a simple python command, and I'd like to keep it inline in a bash command. Basically I 'm reading a JSON string and I want to retrieve a value, but I cannot write a "for loop".

i'll simplify:

string="[{ 'url': 'https://www.example.com/1', 'description': 'URL example number 1', 'name': 'test1', 'id': '1' }, { 'url': 'https://www.example.com/2', 'description': 'URL example number 2', 'name': 'test2', 'id': '2' }]"

echo $string | python3 -c "import sys,json; data= json.load(sys.stdin); for item in data: print(item['id']);"

I get the error

File "<string>", line 1
    import sys,json; data= json.load(sys.stdin); for item in data: print(item['id']);
                                                 ^^^
SyntaxError: invalid syntax

I cannot understand what's wrong, but I found that if "for" is the first command, then it works. so it must be related to indentation, am I right?

thank you

tuffo19
  • 310
  • 3
  • 8
  • https://stackoverflow.com/questions/8236380/why-is-semicolon-allowed-in-this-python-snippet Semicolons are allowed if there are no statements that would start a new code block - and `for` starts such block. However, you could make a fake loop using list comprehension or make one print with a joined string (also using comprehension/generator in the str.join call) – h4z3 Jul 26 '23 at 08:14
  • [Why can't use semi-colon before for loop in Python?](https://stackoverflow.com/questions/24293128/) – Ignatius Reilly Jul 26 '23 at 08:17
  • [This answer](https://stackoverflow.com/a/2043499/11082237) provides a few alternatives you can use to execute python for loops in bash one-liners – Pierre couy Jul 26 '23 at 08:20
  • Also, check the comments to [this answer](https://stackoverflow.com/a/24293199/15032126) – Ignatius Reilly Jul 26 '23 at 08:22
  • Does this answer your question? [Why can't use semi-colon before for loop in Python?](https://stackoverflow.com/questions/24293128/why-cant-use-semi-colon-before-for-loop-in-python) – user1934428 Jul 26 '23 at 08:37
  • "so it must be related to indentation, am I right?": no; see the other comments and answers. Indentation isn't the issue here. – 9769953 Jul 26 '23 at 08:47

2 Answers2

1

It's not allowed by the language as explained here. Just split the input string on multiple lines, e.g.:

$ echo '{"foo":"bar"}' | python3 -c '
import sys,json;
data=json.load(sys.stdin);
for item in data:
  print(item)
'
foo
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • I want to add I managed to write is also as: echo '{"foo":"bar"}' | python -c $'import sys,json;\ndata=json.load(sys.stdin);\nfor item in data:\n print(item);' thank you again, your solution is the best option. – tuffo19 Jul 27 '23 at 09:38
0

It is not recommended to write a Python one-liner as commented by others. If you still wish to do it, you need to tweak the script not to include colon after the for loop:

string='[{ "url": "https://www.example.com/1", "description": "URL example number 1", "name": "test1", "id": "1" }, { "url": "https://www.example.com/2", "description": "URL example number 2", "name": "test2", "id": "2" }]'

echo "$string" | python3 -c 'import sys,json; data = json.load(sys.stdin); [print(item["id"]) for item in data]'

Output:

1
2

Btw json strings must be enclosed by double quotes, not single quoutes.

tshiono
  • 21,248
  • 2
  • 14
  • 22