|
|
This is yet another bash & python hybrid answer. I posted this answer because I wanted to process more complex JSON output, but, reducing the complexity of my bash application. I want to crack open the following JSON object from http://www.arcgis.com/sharing/rest/info?f=json in bash:
{
"owningSystemUrl": "http://www.arcgis.com",
"authInfo": {
"tokenServicesUrl": "https://www.arcgis.com/sharing/rest/generateToken",
"isTokenBasedSecurity": true
}
}
Whilst this approach increase the complexity in the Python function, the bash usage becomes simpler:
function jsonGet {
python -c 'import json,sys
o=json.load(sys.stdin)
k="'$1'"
if k != "":
for a in k.split("."):
if isinstance(o, dict):
o=o[a] if a in o else ""
elif isinstance(o, list):
if a == "length":
o=str(len(o))
elif a == "join":
o=",".join(o)
else:
o=o[int(a)]
else:
o=""
if isinstance(o, str) or isinstance(o, unicode):
print o
else:
print json.dumps(o)
'
}
curl -s http://www.arcgis.com/sharing/rest/info?f=json | jsonGet
curl -s http://www.arcgis.com/sharing/rest/info?f=json | jsonGet authInfo
curl -s http://www.arcgis.com/sharing/rest/info?f=json | jsonGet authInfo.tokenServicesUrl
The output of the above script is:
I added support for arrays, so you can use .length and, if the source is a string array, you can use .join:
curl -s http://www.arcgis.com/sharing/rest/portals/self?f=pjson | jsonGet defaultBasemap.baseMapLayers.length
curl -s http://www.arcgis.com/sharing/rest/portals/self?f=pjson | jsonGet defaultBasemap.baseMapLayers.0.resourceInfo.tileInfo.lods
curl -s http://www.arcgis.com/sharing/rest/portals/self?f=pjson | jsonGet defaultBasemap.baseMapLayers.0.resourceInfo.tileInfo.lods.length
curl -s http://www.arcgis.com/sharing/rest/portals/self?f=pjson | jsonGet defaultBasemap.baseMapLayers.0.resourceInfo.tileInfo.lods.23
Which outputs:
- 1
- [{"scale": 591657527.591555, "resolution": 156543.03392800014, "level": 0}, {"scale": 295828763.795777, "resolution": 78271.51696399994, "level": 1}, {"scale": 147914381.897889, "resolution": 39135.75848200009, "level": 2}, {"scale": 73957190.948944, "resolution": 19567.87924099992, "level": 3}, {"scale": 36978595.474472, "resolution": 9783.93962049996, "level": 4}, {"scale": 18489297.737236, "resolution": 4891.96981024998, "level": 5}, {"scale": 9244648.868618, "resolution": 2445.98490512499, "level": 6}, {"scale": 4622324.434309, "resolution": 1222.992452562495, "level": 7}, {"scale": 2311162.217155, "resolution": 611.4962262813797, "level": 8}, {"scale": 1155581.108577, "resolution": 305.74811314055756, "level": 9}, {"scale": 577790.554289, "resolution": 152.87405657041106, "level": 10}, {"scale": 288895.277144, "resolution": 76.43702828507324, "level": 11}, {"scale": 144447.638572, "resolution": 38.21851414253662, "level": 12}, {"scale": 72223.819286, "resolution": 19.10925707126831, "level": 13}, {"scale": 36111.909643, "resolution": 9.554628535634155, "level": 14}, {"scale": 18055.954822, "resolution": 4.77731426794937, "level": 15}, {"scale": 9027.977411, "resolution": 2.388657133974685, "level": 16}, {"scale": 4513.988705, "resolution": 1.1943285668550503, "level": 17}, {"scale": 2256.994353, "resolution": 0.5971642835598172, "level": 18}, {"scale": 1128.497176, "resolution": 0.29858214164761665, "level": 19}, {"scale": 564.248588, "resolution": 0.14929107082380833, "level": 20}, {"scale": 282.124294, "resolution": 0.07464553541190416, "level": 21}, {"scale": 141.062147, "resolution": 0.03732276770595208, "level": 22}, {"scale": 70.5310735, "resolution": 0.01866138385297604, "level": 23}]
- 24
- {"scale": 70.5310735, "resolution": 0.01866138385297604, "level": 23}
|
|
|
answered Apr 26 '17 at 7:30
|
|
grep -Po '"'"version"'"\s*:\s*"\K([^"]*)' package.json. This solves the task easily & only with grep and works perfectly for simple JSONs. For complex JSONs you should use a proper parser. – diosney Nov 17 '14 at 22:14