5

When I run a command I get a response like this

{

    "status": "available",
    "managed": true,
    "name":vdisk7,
    "support":{
    "status": "supported"
    },
    "storage_pool": "pfm9253_pfm9254_new",
    "id": "ff10abad"-2bf-4ef3-9038-9ae7f18ea77c",
    "size":100
},

and hundreds of this type of lists or dictionaries I want a command that does such sort of a thing

if name = "something", 
    get the id

Any links that would help me in learning such sort of commands would be highly appreciated

I have tried

awk '{if ($2 == "something") print $0;}'

But I think the response is in Json so the colum wise awk formatting is not working.

Also it's just a single command that I need to run so I would prefer not to use any external library.

3
  • Have you looked at jq ? It has a convenient syntax for filtering. Commented Oct 22, 2013 at 7:54
  • See also Unix command-line JSON parser Commented Oct 18, 2015 at 13:33
  • For AWK-based JSON parsing see JSON.awk Commented Oct 18, 2015 at 13:34

1 Answer 1

14

JSON parser is better for this task

awk and sed are utilities to parse line-oriented text, but not json. What if your json formatting will change ? (some lines will go on one line ?).

You should use any standard json parser out there. Or use some powerful scripting language, such as PHP, Python, Ruby, etc.

I can provide you with example on how to do it with python.

What if I can't use powerful scripting language ?

If you totally unable to use python, then there is utility jq out there: link

If you have some recent distro, jq maybe already in repositories (example: Ubuntu 13.10 has it in repos).

I can use python!

I would do that using simple python inline script.

For example we have some some_command that returns json as a result.

We have to get value of data["name"].

Here we go:

some_command | python -c "import json, sys; print json.load(sys.stdin)['name']"

It will output vdisk7 in your case

For this to work you need to be sure, json is fully valid.

If you have a list of json objects:

[
  {
    ...
    "name": "vdisk17"
    ...
  },
  {
    ...
    "name": "vdisk18"
    ...
  },
  {
    ...
    "name": "vdisk19"
    ...
  },
...
]

You could use some list comprehensions:

some_command | python -c "import json, sys; [sys.stdout.write(x['name'] + '\n') for x in json.load(sys.stdin)]"

It will output:

vdisk17
vdisk18
vdisk19
Sign up to request clarification or add additional context in comments.

5 Comments

That works. But I am not allowed to use Python, simple bash commands like awk, grep , sed, head , tail , you get the idea.
What limits you from using python ? Python is in LSB (linux standard base). So it should be available on every linux distro.
@Waterlink Would that work in AIX too ??I can't test on AIX right now but I might need to do that in a few days.
@MAD_ABOUT_JAVA I got your problem here. Then you totally unable to use python ) You can try to build jq from source.
Yes, that's better. Thanx for the help Waterlink.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.