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":{}}
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:
in general, to get text between two patterns/strings:
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
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.
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.
The original solution will work for all the 4 other files.
Here is one way with
awk
: