#!/bin/bash | |
function jsonval { | |
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop` | |
echo ${temp##*|} | |
} | |
json=`curl -s -X GET http://twitter.com/users/show/$1.json` | |
prop='profile_image_url' | |
picurl=`jsonval` | |
`curl -s -X GET $picurl -o $1.png` |
This comment has been minimized.
This comment has been minimized.
Nice works like a charm! |
This comment has been minimized.
This comment has been minimized.
Very nice. Thank you so much. To return only the field value, just add " | cut -d":" -f2| sed -e 's/^ *//g' -e 's/ *$//g' " to the end of commands string: function jsonval { |
This comment has been minimized.
This comment has been minimized.
At a quick glance, looks like some things could be done a little more efficiently -- for example instead of starting up multiple sed processes, just use one and multiple -e arguments to it. And why is awk defined with a -v variable named k that is not used within the awk? |
This comment has been minimized.
This comment has been minimized.
tks guys, |
This comment has been minimized.
This comment has been minimized.
It could far more Easy to do and read with less code as well. Key for what you want to findnum occurrence of value if it multiple timefunction jsonValue() { To print your values give second argumentcurl -s -X GET http://twitter.com/users/show/$1.json | jsonValue profile_image_url 1 To print all values don't give second argumentcurl -s -X GET http://twitter.com/users/show/$1.json | jsonValue profile_image_url |
This comment has been minimized.
This comment has been minimized.
Another Example function jsonValue() { URL Array tourlArray=( "curl -s -X GET http://twitter.com/users/show/$1.json" Run below command with for loopecho |
This comment has been minimized.
This comment has been minimized.
^ Thanks |
This comment has been minimized.
This comment has been minimized.
how to replace the value of a json node in a file? |
This comment has been minimized.
This comment has been minimized.
Could you speak as how this is licensed? An important detail for those wanting to integrate this with existing code :) |
This comment has been minimized.
This comment has been minimized.
Perfect, Thanks! |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
Thanks for that simpler snippet @itstayyab! awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p |
This comment has been minimized.
This comment has been minimized.
Awesome, thanks! |
This comment has been minimized.
This comment has been minimized.
Great, thanks!!! |
This comment has been minimized.
This comment has been minimized.
+1 with the kudos! |
This comment has been minimized.
This comment has been minimized.
I think sed grouping could also be used, something like:
And you can probably tweak it so the grouping regex is just |
This comment has been minimized.
This comment has been minimized.
How about |
This comment has been minimized.
This comment has been minimized.
hello, i tried but it only return one instance value, my json file has multiple values under same prop names (play_url) thanks |
This comment has been minimized.
This comment has been minimized.
I found this Github project incredibly useful for JSON parsing: |
This comment has been minimized.
This comment has been minimized.
Thanks for this, saved my day ;) |
This comment has been minimized.
This comment has been minimized.
I have the same issue as @ikelca mentioned, any solutions by using this script? |
This comment has been minimized.
This comment has been minimized.
none of these solutions work for a json response that includes a URL (i.e: https://some.url") {"response":{"code":"200","message":"OK"},"item":"https://someurl.com/api/"} |
This comment has been minimized.
This comment has been minimized.
works great, thanks |
This comment has been minimized.
This comment has been minimized.
great, thx. |
This comment has been minimized.
This comment has been minimized.
Hi, if i have a json file or json string, and then i need to extract all the key from it, then what changes i need to do? thanks in advance. |
This comment has been minimized.
This comment has been minimized.
gnar gnar |
This comment has been minimized.
This comment has been minimized.
Genius, thanks |
This comment has been minimized.
This comment has been minimized.
Great tips and examples, saved the evening :)... Thanks! |
This comment has been minimized.
This comment has been minimized.
Or just use jq... |
This comment has been minimized.
This comment has been minimized.
jq is that bomb |
This comment has been minimized.
This comment has been minimized.
@itstayyab, |
This comment has been minimized.
This comment has been minimized.
Small modification to display only one result for our $KEY.
But... How to display whole result if it contains "," or ":" character? |
This comment has been minimized.
This comment has been minimized.
@psmanek. I am facing similar issue. my value string contains , and =. Please let me know if you or anyone on this thread were able to resolve this. |
This comment has been minimized.
This comment has been minimized.
If my json contains a list of object, how can I extract 2 properties of each object? |
This comment has been minimized.
This comment has been minimized.
nice. works very well. |
This comment has been minimized.
This comment has been minimized.
Great it works very well with adding --silent option in curl |
This comment has been minimized.
This comment has been minimized.
it works! tkz, great job! |
This comment has been minimized.
This comment has been minimized.
what do I need to do, to get 'foo' and 'bar' ? |
This comment has been minimized.
This comment has been minimized.
I would advise to use jq, like this : cat file.json | jq .array would give: |
This comment has been minimized.
This comment has been minimized.
Functionally same as @itstayyab, but Yes, i know, pretty late to the party. |
This comment has been minimized.
This comment has been minimized.
Works perfectly. Thanks |
This comment has been minimized.
This comment has been minimized.
Just got the notification, glad it helped you. I have made some improvements since the last time. ###################################################
# Method to extract specified field data from json
# Globals: None
# Arguments: 2
# ${1} - value of field to fetch from json
# ${2} - Optional, nth number of value from extracted values, by default shows all.
# Input: file | here string | pipe
# _json_value "Arguments" < file
# _json_value "Arguments <<< "${varibale}"
# echo something | _json_value "Arguments"
# Result: print extracted value
###################################################
_json_value() {
declare LC_ALL=C num="${2:-}"
grep -o "\"""${1}""\"\:.*" | sed -e "s/.*\"""${1}""\": //" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/\"//" -n -e "${num}"p
} To avoid forking of multiple sed processes |
This comment has been minimized.
This comment has been minimized.
Got my, mentioned after 6 years :) Glad that I contributed to something. |
This comment has been minimized.
This comment has been minimized.
Awesome bit of code. Thank you saved me a ton of time and frustrations. Why there is no string compare like a contains just baffles me. The heavy handed use of grep is so silly. All I wanted was a simple if (something.contains(this)); then this allows me to achieve this by parsing the JSON response and then very easily create the if statement. Thank you! You are a time saver! |
This comment has been minimized.
A bash script which demonstrates parsing a JSON string to extract a property value. The script contains a jsonval function which operates on two variables, json and prop. When the script is passed the name of a twitter user it attempts to download the user's profile picture.