Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dahjelle/10934744 to your computer and use it in GitHub Desktop.
Save dahjelle/10934744 to your computer and use it in GitHub Desktop.
Testing JSON APIs from my editor

I was working on a JSON API (for IconCMO) and realized that it'd be nice to be able to see the output of the API in my editor. In particular, I not only wanted the JSON output pretty-printed, but also wanted to highlight a few specific keys of the output, as that's what my code was focusing on.

Most editors let you execute shell commands in one way or another, and display their output. Here's the command I used (the URL is local-only) and its output:

OUTPUT=$(curl --request POST --data '{"Auth": {"Phone": "5555555555", "Username": "JoePUser", "Password": "password"}, "Request": {"Module": "payroll", "Section": "payment_method_form"} }' --header 'Content-type:application/json' --silent https://david.iconcmo.com/api/); echo $OUTPUT | jq '.payment_method_form.test'; echo $OUTPUT
{
  "requestid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "sessionid": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
}

{ "role": "staff", "session": "zzzzzzzzzzzzzzzzzzzzzzzzzz", "permissions": { "delete": true, "update": true, "read": true, "create": true }, "payment_method_form": { "test": { "requestid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "sessionid": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" } } }

This way, I can easily see both the parts of the response I'm interested in, as well as the whole response (in case, for instance, there was a invalid JSON response).

Thanks to the spiffy jq tool to parse JSON on the command-line!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment