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

Bharani <>
Tue Aug 19 09:15:58 CEST 2008


Pattern matching is OK if you have a small string. In my case i am parsing
the json returned by Solr ( text search engine built on top of lucene) and
the code doesn't look compact if you use the pattern matching syntax. I just
wish i could use objects and fields like what javascript does. Then again
JSON is for javascript and not Erlang - i am just trying to find out the
best approach.

Thanks for all your comments

-Bharani



daws wrote:
> 
> 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
> 
> -----
> "The best way to predict the future is to invent it." - Alan Kay
> 

-- 
View this message in context: http://www.nabble.com/javascript-.-notation-to-fetch-objects-from-JSON-decode_string-1-tp18962077p19045429.html
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list