0

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.

Bikash Gyawali
  • 969
  • 2
  • 15
  • 33
  • 1
    Do really want JSON or a JavaScript object? – Felix Kling Mar 14 '11 at 09:40
  • Just expanding on Felix's comment: My bet is you really want a JavaScript object, so you can interact easily with its properties. Converting from your given notation to a JavaScript object has nothing whatsoever to do with JSON. JSON is just a notation (e.g., text). You're already using a different notation (text). – T.J. Crowder Mar 14 '11 at 09:55
  • 1
    I **really** wish people wouldn't ask-and-run. Stick around for at least five minutes to clarify if required. – T.J. Crowder Mar 14 '11 at 09:58
  • @TJCrowder: I totally agree with you. I also wish that people learn the difference between JSON and JS object literals :-/ – Felix Kling Mar 14 '11 at 10:04
  • Sorry that i was lost. I actually need the JSON from the javascript string because I need to feed some other function that can only take JSON as input and not javascript. – Bikash Gyawali Mar 14 '11 at 10:09
  • @Felix: In this case, your wish is granted! :-) @Bikash: Thanks for clarifying. – T.J. Crowder Mar 14 '11 at 10:11
  • Ok... but I'm still a bit uncertain because you wrote `JSON Object` in the title ;) So to be clear: You have a string as input and you want to transform it into another string in JSON format? – Felix Kling Mar 14 '11 at 10:16
  • Yes, I have a javascript string and want a JSON string. I will edit the title to say JSON string, not object. – Bikash Gyawali Mar 14 '11 at 10:20
  • @Bikash: Thanks. Sorry for being so nitpicky, but there are a lot of questions with confusion about JSON and JS object literals. This makes one very suspicious ;) – Felix Kling Mar 14 '11 at 10:22
  • @Felix: Thanks for those remarks. I actually realized that I had a confusing title after you put those remarks. – Bikash Gyawali Mar 14 '11 at 10:24

1 Answers1

1

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Ok, I will go for my own. The string I need is actually like '{"a": [{"b": "b", "c": "c"}], "d": [{"e": "e"}]}'. – Bikash Gyawali Mar 14 '11 at 10:22
  • @Bikash: You want `a` and `d` to both be arrays with exactly one entry, where the entry is an object with properties for the contents of the `[]` in the original? How...interesting... – T.J. Crowder Mar 14 '11 at 10:24
  • sorry, it should have been : '{"a": [{"b": "b"}, {"c": "c"}], "d": [{"e": "e"}]}'. Of course, this is just a sample i put to just represent the format I needed. The key and value being the same doesn't make sense for me too ;) – Bikash Gyawali Mar 14 '11 at 10:29
  • Well, as T.J. suggested, I went for a string->string conversion and then used the solution available at http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java . – Bikash Gyawali Mar 14 '11 at 12:18