简体   繁体   English

使用Python从BASH CURL Response获取JSON值

[英]Get JSON value from BASH CURL Response with Python

I am having a problem parsing a JSON response to 2 variables in BASH. 我在BASH中解析对2个变量的JSON响应时遇到问题。 I dont have access to install jq or jsawk or anything cool that makes life easier. 我没有权限安装jq或jsawk或任何酷,让生活更轻松。 I have python, thats all. 我有python,就是这样。

This is what I'm working with: I have a curl call that gets a JSON response. 这就是我正在使用的:我有一个获得JSON响应的curl调用。 The response is stored in a variable called api_response. 响应存储在名为api_response的变量中。

API_RESPONSE=$(curl --silent -v -H "Content-Type: application/json" -H "MY-Token: $Token" -XPOST -d "$INPUTS" ${MY_BASE}$MY_PROCESS${PROCESS})

this variable essentially is the value of the response from the api 这个变量基本上是来自api的响应的值

[{"name":"test-name1", "value" : "test-value1"},{"name" : "test-name2","value" : "test-value2"}]

In the past, I had only need to get a single value from the response and was able to do that using the following 在过去,我只需要从响应中获取单个值,并且能够使用以下内容执行此操作

API_RESPONSE=$(curl --silent -v -H "Content-Type: application/json" -H "MY-Token: $Token" -XPOST -d "$INPUTS" ${MY_BASE}$MY_PROCESS${PROCESS} | python -c "import sys, json; print json.load(sys.stdin)[1]['value'])

[outputs] [输出]

test-value2

I was trying to extract to two JSON values from the single variable API_RESPONSE but I get an error doing it this way. 我试图从单个变量API_RESPONSE中提取两个JSON值,但是这样做会出错。

API_RESPONSE=$(curl --silent -v -H "Content-Type: application/json" -H "MY-Token: $Token" -XPOST -d "$INPUTS" ${MY_BASE}$MY_PROCESS${PROCESS})

myvar1=$($API_RESPONSE | python -c "import sys, json; print json.load(sys.stdin)[0]['value']")
myvar2=$($API_RESPONSE | python -c "import sys, json; print json.load(sys.stdin)[1]['value']")

I get the following error: 我收到以下错误:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 267, in load 
parse_constant=parse_constant, **kw)
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

The variable api_response is the same data that was working before. 变量api_response与以前工作的数据相同。 Why would it work with a curl call and not from variable? 为什么它会使用curl调用而不是变量?

I figured it out with help from a friend. 我在朋友的帮助下弄清楚了。

When going from bash variable to python sys.stdin, a bash variable needs to be redirected by SOME action to the python sys.stdin. 当从bash变量转到python sys.stdin时,需要通过SOME操作将bash变量重定向到python sys.stdin。

With the curl execution piping output to the python sys.stdin it would work because the output was being redirected to python. 使用curl执行管道输出到python sys.stdin它可以工作,因为输出被重定向到python。 But once I had the entire response stored in the variable within bash, the next step was to echo the output and redirect to python. 但是,一旦我将整个响应存储在bash中的变量中,下一步就是回显输出并重定向到python。

[json response] [json回应]

[{"name":"test-name1", "value" : "test-value1"},{"name" : "test-name2","value" : "test-value2"}]

[code block] [代码块]

API_RESPONSE=$(curl --silent -v -H "Content-Type: application/json" -H "MY-Token: $Token" -XPOST -d "$INPUTS" ${MY_BASE}$MY_PROCESS${PROCESS})

myvar1=$( echo $API_RESPONSE | python -c "import sys, json; print json.load(sys.stdin)[0]['value']")
myvar2=$( echo $API_RESPONSE | python -c "import sys, json; print json.load(sys.stdin)[1]['value']")
echo $myvar1
echo $myvar2

[outputs] [输出]

test-value1
test-value2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM