Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在shell脚本中读取JSON数据_Json_Shell_Awk_Grep - Fatal编程技术网

在shell脚本中读取JSON数据

在shell脚本中读取JSON数据,json,shell,awk,grep,Json,Shell,Awk,Grep,我有一个JSON文件,其中包含一些图像的数据: { "imageHeight": 1536, "sessionID": "4340cc80cb532ecf106a7077fc2a166cb84e2c21", "bottomHeight": 1536, "imageID": 1, "crops": 0, "viewPortHeight": 1296, "imageWidth": 2048, "topHeight": 194,

我有一个JSON文件,其中包含一些图像的数据:

{
    "imageHeight": 1536,
    "sessionID": "4340cc80cb532ecf106a7077fc2a166cb84e2c21",
    "bottomHeight": 1536,
    "imageID": 1,
    "crops": 0,
    "viewPortHeight": 1296,
    "imageWidth": 2048,
    "topHeight": 194,
    "totalHeight": 4234
}
我希望在shell脚本中以简单的方式处理这些值。我在网上搜索,但找不到任何简单易懂的材料

编辑:我希望如何处理这些值

我正在使用convert(Imagemagick)处理图像。所以,整个工作流程是这样的。从json文件中的一行中读取一个条目,比如裁剪,然后在裁剪图像时使用该值:

convert-crop[image width from json]x[image height from json]+0+[crop value from json][session_id from json]-[imageID from json].png[sessionID]-[imageID]-croped.png

我建议使用。例如,要获取
图像高度
,可以使用:

jq ".imageHeight" data.json
输出:

1536

如果要将值存储在shell变量中,请使用:

variable_name=$(jq ".imageHeight" data.json)
Python解决方案

导入json
从pprint导入pprint
json_data=open('json_文件')
data=json.load(json_数据)
pprint(数据)
数据['bottomHeight']
输出:

In [28]: pprint(data)
{u'bottomHeight': 1536,
 u'crops': 0,
 u'imageHeight': 1536,
 u'imageID': 1,
 u'imageWidth': 2048,
 u'sessionID': u'4340cc80cb532ecf106a7077fc2a166cb84e2c21',
 u'topHeight': 194,
 u'totalHeight': 4234,
 u'viewPortHeight': 1296}

In [29]: data['bottomHeight']
Out[29]: 1536

我高度评价在shell中使用JSON的
jq
:使用python或perl是一种选择吗?它们都非常支持使用json文件。awk、grep或shell都不支持json支持。您也可以用python提供解决方案,这可能会有所帮助:)…另外,您希望如何处理这些值,即您的预期结果是什么?请更新您的问题。现在我们只是猜测。@cbuckley请您提供一个关于如何处理json文件行中的字段的示例(非常感谢!谢谢非常感谢您的帮助:)