jsonerlangjiffy

How to read a key value after decoding json in erlang


Here is a short query

In Erlang I parsed json using

Ccode = jiffy:decode(<<"{\"foo\": \"bar\"}">>).

it returns

{[{<<"foo">>,<<"bar">>}]}

Now target is to get value of 'foo' and it should return 'bar'

any help is appreciated.


Solution

  • You can extract a list of attributes of the JSON object using pattern matching and then find a value by a key in the resulting list:

    {Attrs} = jiffy:decode(<<"{\"foo\": \"bar\"}">>),
    FooValue = proplists:get_value(<<"foo">>, Attrs).