Reputation: 1048
I have a javascript string representing a tree structure as [a[b,c]d[e]] -- meaning that the tree has 2 top level nodes a and d (a has 2 subnodes b and c & d has a subnode e).
I want a JSON representation of the above tree. (The key could be the same as value). I want to do the transformation programmatically for any number of nodes and subnodes.
I want to know if there exists some code that I can reuse.
Upvotes: 0
Views: 1444
Reputation: 1074285
Negative answers are always tricky, but if you're looking for pre-existing code that will turn this string
'[a[b,c]d[e]]'
into this string
'{"a": ["b", "c"], "d": ["e"]}'
or this string (I couldn't tell which)
'{"a": {"b": "b", "c": "c"}, "d": {"e": "e"}}'
or similar, I think the answer is no, you'll have to write the conversion yourself. Won't be hard, probably don't even need to use regexp except maybe to match identifiers.
You can either go for a straight string->string conversion (again, looks fairly easy), or you can convert your notation into an object, and then use JSON.stringify
from json2.js or similar to turn it into a JSON string. The advantage to the latter method is you don't have to worry about doing the necessary escaping of values, because it becomes the stringifier's problem.
Upvotes: 1