Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl and sed to extract a single element from a short json query

Tags:

regex

bash

curl

sed

I'm working on a short bash script to grab a JSON element from a curl response.

curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false 

returns:

[{"id":0,"seq":0,"raw":"ˈbʌt(ə)n","rawType":"IPA"},{"id":0,"seq":0,"raw":"ˈbʌt(ə)n","rawType":"IPA"}]

I'm trying to extract the "ˈbʌt(ə)n" element.

Though I'm unfamiliar with regex, I think I should be using a substitution with this string:

/.*"(.*)",/

I'm trying to run the following command, but it doesn't seem to work:

curl -H "api_key:[API_KEY]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false | sed /.*"(.*)",\1/

I'm sure there are a few things I'm doing wrong, and after a few hours of searching and reading up on regex and bash I'm out of options.

I don't need to be using sed, I am simply looking for a quick way of doing this in a bash command line so that I can implement it in a TextExpander script on the mac.

like image 649
Chris Kinniburgh Avatar asked Feb 23 '23 22:02

Chris Kinniburgh


2 Answers

Use STRING : REGEXP to extract the value from the json string:

string=$(curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false)
raw=$(expr "$string" : '.*"raw":"\([^"]*\)"')

echo $raw

See man expr:

   STRING : REGEXP
          anchored pattern match of REGEXP in STRING

   Pattern matches return the string matched between \(  and  \)  or null
like image 125
Arnaud Le Blanc Avatar answered Mar 05 '23 17:03

Arnaud Le Blanc


Regular expressions may not be the right thing to use. http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html

On Ubuntu 9.10:

$ sudo apt-get install jsonlib-perl
$ curl -quiet 'http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false' | perl -e 'use JSON; print JSON->new->allow_nonref->decode(<>)->{raw}'
like image 26
jpickard Avatar answered Mar 05 '23 17:03

jpickard