About EasyMorph Tutorials & Examples Web-help

How to send a web request and parse a JSON response


#1

As various web APIs become more widespread we keep getting more and more questions how to access this or that API from EasyMorph. We will be addressing this need with a number of new actions in one of the nearest releases. Until then, as a workaround it is possible to use the PowerShell action with a short script for sending web requests and parsing JSON responses. Here is an example:

$url = "http://free.currencyconverterapi.com/api/v5/convert?q=USD_CAD&compact=y"
(Invoke-WebRequest -Uri $url).Content | ConvertFrom-Json | Select -expand "USD_CAD" | Select -expand "val"

The example sends a GET request to a free web-service to receive a somewhat recent USD/CAD exchange rate. The response comes in as JSON that looks like {"USD_CAD":{"val":1.33933}}. We’re using PowerShell’s command ConvertFrom-Json to parse it and make it an object, which property val is accessed using Select -expand. The value of val is captured back into EasyMorph.

Here is the full example. To make it a bit more interesting, the source and target currencies are defined as EasyMorph parameters and used in the script.

web-api-request.morph (1.6 KB)