[erlang-questions] javascript . notation to fetch objects from JSON decode_string/1

Matt Williamson <>
Thu Aug 14 16:21:40 CEST 2008


Pattern matching:

1> Decoded = mochijson:decode(JSON).
2> ["foo",{"bar":[1,2,{"ugh":MYNUMBER}, 4]}] = Decoded.
["foo",{"bar":[1,2,{"ugh":7}, 4]}]
3> MYNUMBER.
7



On Thu, Aug 14, 2008 at 12:12 AM, Richard A. O'Keefe <>wrote:

> On 14 Aug 2008, at 12:27 am, Bharani wrote:
> > so that i can simple say get(<json string>,"obj1.obj2.field")
> >
> > is this right way to do or can any one point out best practices
>
> If you are *given* things like "obj1.obj2.field", this is not a
> bad way to do it.  But what if you have
>        ["foo",{"bar":[1,2,{"ugh":77},4]}]
> and you want the 77?
>
> For the sake of argument, let's suppose you are using a JSON
> representation where
>
>        number -> number
>        "string" -> <<"string">
>        [X1,...,Xn] -> [X'1,...,X'n]
>        {"l1":X1,...,"ln":Xn} -> [{<<"l1">>,X'1},...,{<<"ln">>,X'n}]
>        {} -> [{}]
>
> Then the path to the 77 would be
>        [2,<<"bar">>,3,<<"ugh">>]
> You _can't_ handle this by splitting "2.bar.3.ugh" into tokens,
> because "2" and "3" are perfectly good field names in JSON.
> Come to think of it,
>        {"obj1.obj2.field":137}
> is a perfectly good JSON object; nothing anywhere says that a
> JSON field label cannot contain dots.
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080814/9a10d688/attachment.html>


More information about the erlang-questions mailing list