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

Richard A. O'Keefe <>
Thu Aug 14 06:12:23 CEST 2008


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.





More information about the erlang-questions mailing list