+ Start a Discussion
SuperfellSuperfell 

jsawk

For everyone that's testing stuff with the command line, check out jsawk, this allows you to parse & process json from the command line. (If you're on a Mac, you can get the spidermonkey dependency via macports)

 

I have this script to get me an oauth token via the username password flow

 

 

curl https://login.salesforce.com/services/oauth2/token -d grant_type=password -d client_id=myClientId -d client_secret=mySecret -d username=foo@example.com -d password=myPassword  -# | ./jsawk "return this.access_token"

 This uses jsawk to extract the access_token value from the response json, using the shell's ` operator, its easy to stick this in a shell variable,

 

export SFDC_SID=`./login.sh`

You can then easily refer to your token when making curl calls to the rest API, e.g.

 

curl -H "Authorization: OAuth $SFDC_SID" http://na1.salesforce.com/services/data/v20.0/recent

 

 

 Of course, you can use jsawk on these as well to process the response, e.g. this will show just the Ids of the records in your MRU

 

 

curl -H "Authorization: OAuth $SFDC_SID" http://na1.salesforce.com/services/data/v20.0/recent | ./jsawk "return this.Id"
["006300000023MvwAAE","a003000000E2BalAAF","0033000000h0k6qAAA","0033000000jWKwJAAW","0033000000jWKwIAAW","0033000000h0k76AAA","0033000000ZsocCAAR","0033000000ZsimSAAR","0033000000PVjEqAAL","01530000000KBIOAA4"]
["006300000023MvwAAE","a003000000E2BalAAF","0033000000h0k6qAAA","0033000000jWKwJAAW","0033000000jWKwIAAW","0033000000h0k76AAA","0033000000ZsocCAAR","0033000000ZsimSAAR","0033000000PVjEqAAL","01530000000KBIOAA4"]

 

 

 

 

jeffdonthemic2jeffdonthemic2

Very sexy! Thanks Simon!

 

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook