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

Hynek Vychodil <>
Thu Aug 14 17:08:05 CEST 2008


Weird, JSON variable has not been bound in first statement. Second statement
raise illegal expression and so.
You have to have got another Erlang than me, isn't it?

2008/8/14 Matt Williamson <>

> 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
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080814/290968e8/attachment.html>


More information about the erlang-questions mailing list