[erlang-questions] json to map

Richard A. O'Keefe ok@REDACTED
Wed Aug 26 07:38:15 CEST 2015


On 26/08/2015, at 12:16 am, Roelof Wobben <r.wobben@REDACTED> wrote:

> Hello,
> 
> As a challenge I need to convert a json file to a erlang map.

You need a more precise specification.
A map with an arbitrary key whose associated value
was a binary containing the text of the JSON term
would technically satisfy this.
> 
> So I have this file :
> 
> {
>    "foo": {
>        "id": 1,
>        "username": "Foo Foo",
>        "first": "Foo",
>        "last": "Foo",
>        "password": "foo",
>        "email": "foo@REDACTED",
>        "scope": ["admin", "user"]
>    },
>    "bar": {
>        "id": 2,
>        "username": "Bar Head",
>        "first": "Bar",
>        "last": "Head",
>        "password": "bar",
>        "email": "bar@REDACTED",
>        "scope": ["user"]
>    }
> }
> 
> 
> as far as I can see it's a tuple of tuples.

No, it's not a tuple of tuples.  To start with,
it's >>JSON<<, not Erlang.  JSON has arrays, not
tuples.

Here's a table that may help

JSON			Erlang
null			null (the atom)
false			false
true			true
a number		the same number
a string		a binary with the UTF-8 encoding
an array ([])		a list
an object ({})		a map

Again, you don't say *how* the conversion is to be done.
You could probably whip something up in a few minutes
using lex to make a C program that basically changed
opening " to <<", closing " to ">>, and did the right
things for { } and : .  That would convert JSON syntax
to Erlang syntax quite quickly.

Probably what you meant was
WEB-SEARCH(JSON parser for Erlang using maps)
This might lead you to https://github.com/talentdeficit/jsx
or to one of the other JSON libraries with which Erlang is
so liberally equipped.






More information about the erlang-questions mailing list