Bash Shell script extract Json object

2019-02-28 08:08发布

In Bash shell script, I want to extract an object. For example, with following json file, I would like to extract dependencies object and it should return me: "dmg": ">= 0.0.0", "build-essential": ">= 0.0.0", "windows": ">= 0.0.0" in whatever format and how do you do that?

// My data 1.json:

{
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    },
    "recommendations": {}
}

// My data 2.json:

{
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "recommendations": {},
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    }
}

// My data 3.json:

{
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    },
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "recommendations": {}
}

// My data 4.json:

{
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    }
}

// My data 5.json (compress):

{"dependencies":{"dmg":">= 0.0.0","build-essential":">= 0.0.0","windows":">= 0.0.0"},"platforms":{"amazon":">= 0.0.0","arch":">= 0.0.0","centos":">= 0.0.0","debian":">= 0.0.0"},"recommendations":{}}

4条回答
可以哭但决不认输i
2楼-- · 2019-02-28 08:42

Have you looked at jsawk? I would generally use python for parsing JSON data on UNIX systems, since it usually comes bundled with the OS.

Anyways, you can try this:

awk "/dependencies/,/}/ { print }" test.json | grep ":" | grep -v dependencies

in general, to get text between two patterns/strings:

awk "/Pattern1/,/Pattern2/ { print }" inputFile

and then use grep ":" to get all the lines containing the ':' in the object, and then filter out the object name itself by getting all the subsequent lines not containing the object name

UPDATE: for json not in pretty format

sed "s/[,{}]/&\n/g" prettified.json | awk "/dependencies/,/}/ { print }" | grep ":" | grep -v dependencies | awk '{$1=$1}1'
查看更多
趁早两清
3楼-- · 2019-02-28 08:44
sed -n '/dependencies/, /}/ p' t|grep '>='


How this works :

First get the text between dependencies block, and then extract the dependencies.

Note that this method is independent of where in the text the dependency block is located. As long as it is present, you'll get the answer.


aman@apollo:~$ sed -n '/dependencies/, /\}/ p' t|grep '>='
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"

Use sed -n '/dependencies/, /}/ p' t|grep '.*='

If there can be symbols like ~=, = in the dependency block (and not just >=).


Compressed version For the compressed version, you can first "decompress" (insert newlines) the file and then apply the same transformation.

sed -e 's/:{/:{\n/g'  -e  's/},/\n},\n/g' d5|sed -n '/dependencies/, /}/ p'|grep '>='

The original solution will work for all the 4 other files.

查看更多
Bombasti
4楼-- · 2019-02-28 08:55

Here is one way with awk:

awk -v RS= -F'},|{' '{print $5}' file | awk 'NF'

$ awk -v RS= -F'},|{' '{print $5}' f | awk 'NF'
    "dmg": ">= 0.0.0",
    "build-essential": ">= 0.0.0",
    "windows": ">= 0.0.0"
查看更多
Lonely孤独者°
5楼-- · 2019-02-28 08:55
$ $ tr -d '\n' < myjson.json | sed -e's/[}{]//g' | sed -e's/.*dependencies\":\(.*\)\s*,.*/\1/g' | sed -e's/^ *//g' | sed -e's/, */, /g'
"dmg": ">= 0.0.0", "build-essential": ">= 0.0.0", "windows": ">= 0.0.0"
查看更多
登录 后发表回答