curl command json response value without jq - json

my curl command returns a json response
{"token":"abcd"}
how do I get the token value into a variable in the shellscript?
i can not use jq something most posts have suggested in the past. The pattern of this response is also set (there will be only 1 key-value pair), so if this response can in anyway be converted to a string then using substring could be a option.

I found this blog, did exactly what I want. Works wonder.

Related

When creating a variable from command output, Bash removes a backslash from the JSON. How do I make it keep both backslashes to maintain valid JSON?

I'm doing the following to capture some ADO JSON data:
iteration="$(az boards iteration team list --team Test --project Test --timeframe current)"
Normally, the output of that command contains a JSON key/value pair like the following:
"path": "Test\\Sprint1"
But after capturing the STDOUT into that iteration variable, if I do
echo "$iteration"
That key/value pair becomes
"path": "Test\Sprint1"
And if I attempt to use jq on that output, it breaks because it's not recognized as valid JSON any longer. I'm very unfamiliar with Bash. How can I get that JSON to remain valid all the way through?
As already commented by markp-fuso:
It looks like your echo command is interpreting the backslashes. You can confirm this by running echo 'a\\b' and looking at the output.
The portable way to deal with such problems is to use printf instead of echo:
printf %s\\n "$iteration"

Extract json object from wget request

I want to do with php exactly the opposite of what James Nine was asking in PHP: How to extract JSON strings out of a string dump.
That is to keep the json object(s) out from a large output from a wget request. I can't figure out what the regex should be, basically negate
~\{(?:[^{}]|(?R))*\}~

Ways to parse JSON using KornShell

I have a working code for parsing a JSON output using KornShell by treating it as a string of characters. The issue I have is that the vendor keeps changing the position of the field that I am intersted in. I understand in JSON, we can parse it by key-value pairs.
Is there something out there that can do this? I am intersted in a specific field and I would like to use it to run the checks on the status of another RESTAPI call.
My sample json output is like this:
JSONDATA value :
{
"status": "success",
"job-execution-id": 396805,
"job-execution-user": "flexapp",
"job-execution-trigger": "RESTAPI"
}
I would need the job-execution-id value to monitor this job through the rest of the script.
I am using the following command to parse it:
RUNJOB=$(print ${DATA} |cut -f3 -d':'|cut -f1 -d','| tr -d [:blank:]) >> ${LOGDIR}/${LOGFILE}
The problem with this is, it is field delimited by :. The field position has been known to be changed by the vendors during releases.
So I am trying to see if I can use a utility out there that would always give me the key-value pair of "job-execution-id": 396805, no matter where it is in the json output.
I started looking at jsawk, and it requires the js interpreter to be installed on our machines which I don't want. Any hint on how to go about finding which RPM that I need to solve it?
I am using RHEL5.5.
Any help is greatly appreciated.
The ast-open project has libdss (and a dss wrapper) which supposedly could be used with ksh. Documentation is sparse and is limited to a few messages on the ast-user mailing list.
The regression tests for libdss contain some json and xml examples.
I'll try to find more info.
Python is included by default with CentOS so one thing you could do is pass your JSON string to a Python script and use Python's JSON parser. You can then grab the value written out by the script. An example you could modify to meet your needs is below.
Note that by specifying other dictionary keys in the Python script you can get any of the values you need without having to worry about the order changing.
Python script:
#get_job_execution_id.py
# The try/except is because you'll probably have Python 2.4 on CentOS 5.5,
# and the straight "import json" statement won't work unless you have Python 2.6+.
try:
import json
except:
import simplejson as json
import sys
json_data = sys.argv[1]
data = json.loads(json_data)
job_execution_id = data['job-execution-id']
sys.stdout.write(str(job_execution_id))
Kornshell script that executes it:
#get_job_execution_id.sh
#!/bin/ksh
JSON_DATA='{"status":"success","job-execution-id":396805,"job-execution-user":"flexapp","job-execution-trigger":"RESTAPI"}'
EXECUTION_ID=`python get_execution_id.py "$JSON_DATA"`
echo $EXECUTION_ID

Parsing JSON DATA in AWS

I still cannot parse JSON data in linux.
I need a linux command to parse Json data to readable string.
someone told me to use underscore-cli.(https://npmjs.org/package/underscore-cli)
I install and use it, still the result is unreadable.
my data:
"2005\u5e7405\u670812\u65e5(\u6728) 02\u664216\u5206"
according to this link
http://json.parser.online.fr/
the result is
"2005年05月12日(木) 02時16分"
Is there any other way to parse this Json data?
Please help.
Try jq: http://stedolan.github.com/jq/
echo '"2005\u5e7405\u670812\u65e5(\u6728) 02\u664216\u5206"' | ./jq .
"2005年05月12日(木) 02時16分"
jq takes escaped unicode and outputs it in utf-8.

Trying to send json array to my json server with curl

I need to identify what JSON values my server expects. I've been doing testing within Haskell, and all is well. So now I need to take the next step so I can document what these values look like for other people to use.
One of my REST methods expects an Array
Array (fromList [String "BNAP",Number 312])
is how Haskell expresses this.
Testing with cURL, I want to do something like this:
curl -D- -X POST -H "Content-Type: application/json" --data '["BNAP":312]'
http://10.64.16.6:3000/Read
But this seems to not be valid JSON
ParseError {errorContexts = ["]"], errorMessage = "Failed reading: satisfyWith", errorPosition = 1:8}
If Haskell was expecting an Object Object (fromList [String "BNAP",Number 312]) for example, the JSON would be expressed this way
--data '{"BNAP":312}'
So to me, it follows that my above attempt would be right. But, it's not. So, how does one express an Array with one Pair consisting of a String and a Number, with cURL?
The colon is not valid JSON here:
["BNAP":312]
^
Array elements should be separated by a comma:
["BNAP", 312]
If you wanted an array with a single key-value pair, you need to add curly braces:
[{"BNAP": 312}]