sse_json module

Functions to serialise and de-serialise JSON.

decode/1

Takes a serialised JSON value and de-serialises it as an Erlang map. The function returns an error if its input is not valid JSON or if it has no Erlang equivalent.

decode(utf8() | string()) ->
  {ok, json_term()}
| {error, reason()}.

The function can take structured input too, such as lists and dictionaries. The example below gets a JSON dictionary as response from an HTTP GET request and transforms it into an Erlang map using the decode/1 function.

<service name="Expr"
    provision="expr"/>
<field name="GET"/>
<field name="bitcoin_map"
    type="term"/>
<mix name="Mix">
    <request name="GetBtc"
        service="Expr"
        fields="GET">
        <prop name="expr.src"
            content-type="text/x-erlang"><![CDATA[

% Get JSON map of Bitcoin values
Request = {"https://blockchain.info/ticker",[]},
{Tag, Result} = httpc:request(get,Request,[],[]),
{Status, Headers, BodyJson} = Result,     

% Turn JSON object into Erlang map and bind it to Term variable
{ok, Term} = sse_json:decode(BodyJson),

% Send Ok reply with bitcoin_map field. 
% expr.bind.out prop binds this field to Term variable
"Ok".

        ]]></prop>
        <prop name="expr.bind.out"
            Term="bitcoin_map"/>
        <reply name="Ok"
            fields="bitcoin_map"/>
    </request>
</mix>
Tip: SPARKL fields enforce their type on the data they carry. For example, the integer 1 sent in a string type field arrives as "1". A JSON type field transforms an Erlang map into a JSON object. If the transformation fails, SPARKL sends a default value in the field.

encode/1

Takes an Erlang term and serialises it as JSON. The function returns an error if its input is not a valid term or if it has no JSON equivalent.

encode(term()) ->
  {ok, utf8()}
| {error, reason()}.

The function can take structured input too, such as lists and Erlang maps. The example below builds an Erlang map, serialises it using the encode/1 function, and sends it to Slack - a messaging application.

Important: The JSON equivalent of an Erlang string is its character list format. For example, the JSON equivalent of the Erlang string "hello" is [104,101,108,108,111]. Use utf8 strings to avoid this behaviour.
<service name="Expr" 
    provision="expr"/>
<field name="message" 
    type="utf8"/>
<mix name="Mix">
    <consume name="SendMessage" 
        service="Expr" 
        fields="message">
        <prop name="expr.bind.in" 
            Message="message"/>
        <prop name="expr.src" 
            content-type="text/x-erlang"><![CDATA[

% Specify a Slack webhook
Url = "https://hooks.slack.com/...",

% Build a Slack message, "text" is a default key expected by Slack
% Message is bound to the input field message
Map = #{
    "text" => Message
},

% Transform the Erlang map into JSON and bind it to the Json variable
{ok, Json} = sse_json:encode(Map),

% Post the Json to Slack through the webhook 
Request = {Url, [], "application/json", Json},
httpc:request(post, Request, [], []).

        ]]></prop>
    </consume>
</mix>
Tip: SPARKL fields enforce their type on the data they carry. For example, the integer 1 sent in a string type field arrives as "1". A JSON type field transforms an Erlang map into a JSON object. If the transformation fails, SPARKL sends a default value in the field.