[erlang-questions] Access JSON in Erlang like you do in JavaScript

Robert Virding <>
Wed Oct 20 04:37:53 CEST 2010


You could always write it in the form "{Obj,post,title}" which is valid syntax. Or do it without parse transforms and call functions with a syntax like:

fetch(Obj, {post,title})

This type of syntax, nesting using tuples, is used in OTP in places to access fields in nested property lists, and it is both legal erlang and not too ugly. Also much easier to implement function than parse transform.

Robert


----- "Ryan Zezeski" <> wrote:

> On Tue, Oct 19, 2010 at 8:24 PM, Jesper Louis Andersen <
> > wrote:
> 
> >
> > Have you considered wrapping the code into a parse transform so you
> > can "embed" the javascript notation into Erlang directly?
> >
> >
> I have, and my first attempt was to use a parse transform.  However, a
> parse
> transform requires that it's valid Erlang syntax and I don't believe
> something like "Obj.post.title" is considered valid Erlang
> syntax--correct?
>  You could do something like "Obj/post/title" but at that point it
> looks
> more similar to XPath.  Maybe I'll play around with this more.
> 
> Another option is to write an xpath-like query tool, perhaps with a
> > Zipper construction on the parse tree so you have a neat
> continuation
> > for the next match, should you need it. This is probably my
> solution,
> > if I need to process JSON documents in the future. You already made
> > the first venture into this - think jQuery DOM selectors on
> steroids
> > for selection into JSON structures.
> >
> 
> Huet's Zipper was definitely in my mind but I didn't do anything with
> it.  I
> think I like the sound of jQuery for JSON but I'm not sure what that
> would
> look like.  Has anyone done this in the JavaScript world?
> 
> Let's say I had an object like so:
> 
> var Obj = { posts: [{title: "Foo", body: "..."}, {title: "Bar", body:
> "..."}, ...]};
> 
> Are you talking about having something like the following...
> 
> $(Obj, "title").each( function() { alert($(this).value); };
> 
> Pardon if my syntax is off, it's been a while since I've done
> JavaScript/jQuery development.


More information about the erlang-questions mailing list