0

I have a JSON file that contains what I believe to be a correct JSON string:

{"title": "exampleTitle", "tipTitle": "exampleTipTitle", "tip": "exampleTip"}

I'm trying to parse said file and take out the 3 values then store them in variables, however currently, it parses each individual character as a separate object, therefore:

JSONobj[1] =  "

and so on. Assuming that currentLocation = the directory location of the json file.

Code

var jsonLocation = currentLocation + "json.txt";
var request = new XMLHttpRequest();
request.open("GET", jsonLocation, false);
request.send(null);
var returnValue = request.responseText;
var JSONobj = JSON.parse(JSON.stringify(returnValue));
var headerTitle = JSONobj[0];

A few clarifications, the stringify is in because it was throwing an unexpected token error. I've tried changing the file tile to .json instead but that also makes no difference. "It also gives off a XMLHttpRequest on the main thread is deprecated" but I'm not particularly sure how to solve that issue. Any help would be appreciated.

1
  • Side note: Avoid passing false as the third argument to open. Instead, embrace the asynchronous nature of web programming. Commented Oct 20, 2015 at 9:32

2 Answers 2

3
var returnValue = request.responseText;

Here returnValue is a string of JSON.

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
var JSONobj = JSON.parse(JSON.stringify(returnValue));

Here you convert the string of JSON to JSON. So you have a JSON string representing a string, and that string is a representation of a data structure in JSON.

"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"

Then you parse it and convert it back to the original string of JSON.

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}

So you end up back where you start.

Just don't use JSON.stringify here, and you'll convert your JSON to a JavaScript object:

var javascript_object = JSON.parse(returnValue);

Then you have an object, but it doesn't have a 0 property so it doesn't make sense to access it with javascript_object[0]. The properties have names, such as javascript_object.title.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, this returned object Object but I can see that my problem was more that I was trying to index something that wasn't actually an array. I appreciate the help.
1

Your JSON doesn't describe an array, so indexing into it with an index like 0 doesn't make sense. Your JSON describes an object, which will have properties with the names title, tipTitle, and tip.

Additionally, you're overdoing your parsing: You just want to parse, not stringify (which is the opposite of parsing):

var JSONobj = JSON.parse(returnValue);

So:

var JSONobj = JSON.parse(returnValue);
var headerTitle = JSONobj.title;
console.log(headerTitle); // "exampleTitle"

Side note: By the time you've assigned it to the variable you've called JSONobj, it isn't JSON anymore, it's just a normal JavaScript object, so that name is a bit misleading. If you're writing source code, and you're not dealing with a string, you're not dealing with JSON anymore. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.