Following is the sample output data in json format when i create a JIRA from command line using curl command.
{"id":"123456","key":"ABCD-123","self":"http://abcd.com/rest/api/2/issue/123456"}
How can i read ABCD-1234 into a variable. i have tried json data parser jq but it didn't help.
Reference Link: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/
With jq:
key=$(jq -r '.key' file)
Related
I have a command that returns a JSON dump that won't get saved into any file.
I have to parse a particular field from the JSON response without saving the output.
I am able to achieve it if I save the output of the command and then parse it using jq and grep like this:
platform json_dump platform_id >resp.json
jq . resp.json | grep elbName
But, I do not want to write the output of my command platform json_dump platform_id which is a JSON dump into any file. I want to parse the elbName directly from the out of the command.
Is there a way I can do that?
Just pipe the program's output to jq:
platform json_dump platform_id | jq .elbName
or whatever.
PS: Use jq to get the value you want, not grep. Example of doing that.:
$ echo '{"elbName":"foo"}' | jq .elbName
"foo"
You can try another pipe to pass the result to jq command
platform json_dump platform_id | jq .elbName
I'm assuming you have python :)
platform json_dump platform_id | python -c 'import sys,json; print(json.load(sys.stdin)["elbName"])' # a bit long ? :)
I have a json file with the following contents
{"STATUS":"PASS","PASSRATE":96.95238}
I want to store the passrate so that I can use it to append to a filename.
Check jsonextractor.bat with which you can extract information from json file by given dot notated path to object:
jsonextractor.bat data.json PASSRATE
You could use Xidel for that:
xidel -s data.json -e "$json/PASSRATE"
How to convert blueprint json file to csv file?
My target is to convert all properties parameters to a csv file from the amabri cluster
Example – how to generate new fresh blueprint.json file from my ambari cluster
curl -u admin:admin -H "X-Requested-By: ambari" -X GET http://10.23.4.122:8080/api/v1/clusters/HDP01?format=blueprint -o /tmp/HDP01_blueprint.json
example of expected results: ( all parameters from json file from all config types should be in the csv file )
autopurge.purgeInterval,512
dataDir,/hadoop/zookeeper
autopurge.snapRetainCount,10
clientPort,2181
initLimit,11
tickTime,2000
syncLimit,5
You could write your own script for doing this conversion.
For example you could use PHP for reading the JSON and creating the csv file exactly the way you want it.
Reading the JSON
$fileContent = file_get_contents('/tmp/HDP01_blueprint.json');
$parsedContent = json_decode($fileContent, true);
After this the content is stored in the $parsedContent variable as an associative array. With this array you can write the values you want to a csv file.
You can even let the script fetch the JSON string for you if you want.
I am getting response from the below command in JSON format in .sh file (using shell script):
**curl https://api.flipkart.net/sellers/skus/$col1/listings -H "Content-Type: application/json" -H "Authorization: Bearer $value"**
Here is the response I am getting:
{"listingId":"LSMSW","skuId":"M7489","fsn":"ACCCQD","attributeValues":{"actual_stock_count":"1","mrp":"199","seller_listing_state":"current","procurement_sla":"2","zonal_shipping_charge":"0","stock_count":"10","local_shipping_charge":"0","listing_status":"ACTIVE","max_order_quantity_allowed":"3","fulfilled_by":"seller","fk_release_date":"2016-08-29 10:00:08","selling_price":"529","inventory_count":"10","national_shipping_charge":"0","sku_id":"M7489"},"listingValidations":null}
Now I want to extract all the value in a separate variable for reuse it
like:
LISTINGID='listing id value'
SKUID='skuId value'
and many more.
If anyone know the answer please comment with explanation.
Here is a solution using jq. If data.json contains the sample data then the following bash script
#!/bin/bash
jq -M -r '
.listingId
, .skuId
, .attributeValues.mrp
' data.json | \
while read -r listingId
read -r skuId
read -r mrp; do
echo "$listingId $skuId $mrp"
done
produces
LSMSW M7489 199
The jq command in the script writes out the specified attributes on separate lines which are read into bash variables by the successive read -r commands. It should be clear how to extend or modify this to meet your requirements – the primary constraint is that for each line written by jq there should be a corresponding read -r.
I am not aware of any bash json parsers. I would go with Python or Php and parse the Json string in one of these languages and export values from there. In Php there is the putenv() method.
Good luck!
Edit:
Check here
Parsing JSON with Unix tools
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.