Parsing Json data columnwise in shell

后端 未结 1 1295
南方客
南方客 2020-12-21 00:26

When I run a command I get a response like this

{

    \"status\": \"available\",
    \"managed\": true,
    \"name\":vdisk7,
    \"support\":{
    \"status\         


        
相关标签:
1条回答
  • 2020-12-21 00:29

    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
    
    0 讨论(0)
提交回复
热议问题