0

I am using JSON within my javascript and I am trying to get a string value for the dot notation representation.

For example AAA.BBB[0].CCC.DDD[5].EEE = 123 in JSON dot notation format.

But I want to get the value AAA.BBB[0].CCC.DDD[5].EEE as a String so I can save it for later use to allow me to modify my JSON code directly.

Is there a method in Javascript or jQuery that can return a string value representation?

*EDIT I am converting JSON data into a list structure, and I want to save the AAA.BBB[0].CCC.DDD[5].EEE format as the id so when a user modifies the content of that list item it modifies the JSON data directly. Would there be a better way to save the location in the id?

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • A bit more informative than adeneo's answer perhaps: http://www.json.org/js.html – 11684 Mar 29 '13 at 17:23
  • 1
    There is no such thing as "JSON dot notation format". – gen_Eric Mar 29 '13 at 17:26
  • I am converting JSON data into a list structure, and I want to save the "AAA.BBB[0].CCC.DDD[5].EEE" format as the id so when a user modifies the content of that list item it modifies the JSON data directly. Would there be a better way to save the location in the id? – judomonkeykyle Mar 29 '13 at 17:44
  • What do you mean by "modifies the JSON data directly"? What *exactly* are you working with here? JSON is a *string representation* of data. Do you actually have a JSON string, or do you just have a JavaScript object? – gen_Eric Mar 29 '13 at 17:55
  • I have a JSON object that essentially contains a huge nested structure. Currently its being outputted as a html list. I want to set it up so that when a specific node is clicked and then modified by the user, the json code is updated at the given node (which is a location saved to the id of the li) – judomonkeykyle Mar 29 '13 at 17:59

3 Answers3

3

Why not just store a reference to an inner-portion of it? Storing it as a string doesn't make much sense. If you just want a shorter way of accessing it, storing a reference makes more sense.

var theCollection = AAA.BBB[0].CCC.DDD[5];
alert( theCollection.EEE );

this reference can then be stored on an element and retrieved later.

$(someelement).data("jsonref",theCollection);
var data = $(someelement).data("jsonref");
alert(data.EEE);
data.EEE = "foobar";
data = $(someelement).data("jsonref");
alert( data.EEE );
Kevin B
  • 94,570
  • 16
  • 163
  • 180
1
var j_string = JSON.stringify(AAA.BBB[0].CCC.DDD[5].EEE);

If you just want the value returned as a string do this:

var j_string_value = AAA.BBB[0].CCC.DDD[5].EEE.toString();
Ryan
  • 5,644
  • 3
  • 38
  • 66
0

You could probably use eval on the string if you are looking to execute an operation on that path, but eval is pretty dangerous since it allows arbitrary code execution. If you want to JSON stringify the entire structure, there is JSON.stringify which is a standard feature in most new browsers and Doug Crockford has his own take on it for older browsers here.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • The console allows arbitrary code execution, even if you don't include any `eval` calls in your program. – 11684 Mar 29 '13 at 17:39