[erlang-questions] How to access a Map returned by jiffy:decode()

Matthew Fitzpatrick matthew.fitzpatrick6012@REDACTED
Sat Jul 25 15:30:12 CEST 2015


Hey avinash,

By default, Jiffy returns "EJSON", not a map. It looks liked you need to
use an Option for Jiffy to return a map.

```erlang
Tx=jiffy:decode(<<"{\"foo\": \"bar\",\"key\": \"val\"}">>, [return_maps]).
```

jiffy:decode/1,2

   - jiffy:decode(IoData)
   - jiffy:decode(IoData, Options)

The options for decode are:

   - return_maps - Tell Jiffy to return objects using the maps data type on
   VMs that support it. This raises an error on VMs that don't support maps.
   - {null_term, Term} - Returns the specified Term instead of null when
   decoding JSON. This is for people that wish to use undefined instead of
   null.
   - use_nil - Returns the atom nil instead of null when decoding JSON.
   This is a short hand for {null_term, nil}.
   - return_trailer - If any non-whitespace is found after the first JSON
   term is decoded the return value of decode/2 becomes {has_trailer,
   FirstTerm, RestData::iodata()}. This is useful to decode multiple terms
   in a single binary.
   - {bytes_per_red, N} where N >= 0 - This controls the number of bytes
   that Jiffy will process as an equivalent to a reduction. Each 20 reductions
   we consume 1% of our allocated time slice for the current process. When the
   Erlang VM indicates we need to return from the NIF.
   - {bytes_per_iter, N} where N >= 0 - Backwards compatible option that is
   converted into the bytes_per_red value.


On Sat, Jul 25, 2015 at 7:22 AM, avinash D'silva <evnix.com@REDACTED>
wrote:

> I have the following code.
>  Tx=jiffy:decode(<<"{\"foo\": \"bar\",\"key\": \"val\"}">>).
>
> where Tx is:
> {[{<<"foo">>,<<"bar">>},{<<"key">>,<<"val">>}]}
>
> I tried the following:
>
> maps:get("foo",Tx).
> ** exception error:
> {badmap,{[{<<"foo">>,<<"bar">>},{<<"key">>,<<"val">>}]}}
>      in function  maps:get/2
>         called as maps:get("foo",
>                            {[{<<"foo">>,<<"bar">>},{<<"key">>,<<"val">>}]})
>
> Any Idea on what I am doing wrong? any possible solutions?
> I Couldn't find any documentation to access this type of map.
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150725/c4e0b4b5/attachment.htm>


More information about the erlang-questions mailing list