-2

I am trying to extract some particular data from the JSON so i wrote the following code

echo "$Group_ID" | python -c 'import json,sys;obj=json.load(sys.stdin); for o in obj: if o[name] == "Admin_UserGroup": print o["id"]';

But its throwing error

Can someone please help and tell me what wrong with the code?

   File "<string>", line 1
     import json,sys;obj=json.load(sys.stdin); for o in obj: if o["name"] == "Admin_UserGroup": print o["id"]
                                                 ^
SyntaxError: invalid syntax

Here is the version details

[root@mdfdevha1 ~]# python -V
Python 2.7.5

EDIT 1 : Attaching the image

enter image description here

14
  • Do you use Python 2? Commented Aug 1, 2018 at 6:51
  • Did you try executing it as a normal script (not on a single line)? Commented Aug 1, 2018 at 6:52
  • @MartinThoma I updated version information and yes i am trying it in single line . Commented Aug 1, 2018 at 6:54
  • @U9-Forward In this example w3schools.com/python/python_for_loops.asp they wrote same. Commented Aug 1, 2018 at 6:57
  • 1
    @AdamSmith Ok i will see the feasibility for my requirement to write python code in a file. Commented Aug 1, 2018 at 7:23

2 Answers 2

2

First, you should also post the Error - 'Syntax Error'

Also, this will be helpful: One-line list comprehension: if-else variants

Therefore, this python code should work for you:

import json ,sys;
obj = json.load(sys.stdin);
print ('\n'.join([str(o["id"]) if o[name] == "Admin_UserGroup" else "" for i in obj]).replace('\n\n','\n').strip('\n'))

Shrink it into a one liner:

echo "$Group_ID" | python -c "import json,sys;obj=json.load(sys.stdin);print ('\n'.join([str(o['id']) if o[name] == 'Admin_UserGroup' else '' for i in obj]).replace('\n\n','\n').strip('\n'))";

EDIT: After 10 minutes of bug fixing, this works!
Let's print the even numbers in 1 - 10:

print('\n'.join([str(i) if i % 2 == 0 else '' for i in range(1, 11)]).replace('\n\n','\n').strip('\n'))

Which outputs as expected

EDIT 2: you cannot use single quotes in your python command

Edit 3: Working command

[root@mdfdevha1 ~]# echo "$Group_ID" | python -c "import json,sys;obj=json.load(sys.stdin);print ('\n'.join([str(i['id']) if i['name'] == 'Admin_UserGroup' else '' for i in obj]).replace('\n\n','\n').strip('\n' ))";
Sign up to request clarification or add additional context in comments.

13 Comments

I'm not sure how this is different.
It's different because it works
@KGSHbteamMineTeamBeastO_ If whole command in one line its a issue in Python ,as i dont know python .
Since the code is throwing an error on the for loop (even with your code) I beg to differ.
There's no reason to use the if pred then a else b idiom here. Note that print number is also equal None (with a side effect). Just if number % 2: print number is fine -- it just doesn't work in this context (after an import statement and a semicolon in a one-line expression)
|
0

The key of a JSON should be given within quotes.

Try this:

echo "$Group_ID" | python -c 'import json,sys;obj=json.load(sys.stdin); for o in obj: if o["name"] == "Admin_UserGroup": print o["id"]'

Thanks

9 Comments

Same exception from above command as well.
Try removing the semicolons in the python script. That could help.
@SmashGuy that's not valid Python anymore, then. You're grasping at straws -- test something.
Please test your code before posting...
@AdamSmith Thanks for the new idiom. But i have never seen semicolon before as i have not worked in Python 2x.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.