5

I have a file.json with contents:

{
  "a":{"var1":"Sábado"},
  "b":{"var2":"Domingo"}
}

Having in mind that I cannot edit file.js, I need to figure out a way to load the json contained in that file into a variable mj, so that alert(mj["a"].var1) shows me the message Sábado.

UPDATE: Is this possible to accomplish without using JQuery, Prototype, or any other js library?

4
  • Are you using node.js, or is this supposed to run in a browser ? Commented Aug 17, 2013 at 23:07
  • If you are using JQuery you can do api.jquery.com/jQuery.getJSON Commented Aug 17, 2013 at 23:09
  • BTW, the name file.js is ambiguous, it should be file.json Commented Aug 17, 2013 at 23:17
  • I edited the question and file name. It has to be a solution that does not use JQuery, node.js or other js library Commented Aug 17, 2013 at 23:19

3 Answers 3

1

This is plain js,(with no use of eval) and should even work cross domain:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "file.js"; /*url of your js/jsonp file */
var head = document.getElementsByTagName('head')[0];
head.insertBefore(script, head.firstChild);
Sign up to request clarification or add additional context in comments.

4 Comments

ok, you have loaded the json file, but how do you store the contents of the json file in variable mj?
you wouldn't need mj (unless you are the Chicago Bulls:), just alert(a.var1)... however may be problematic if you have globals (or local variables) called a, b, ... that match the json.
This would work great if the file was formatted for JSONP. But, without the padding, {...} will be parsed as a block, making : unexpected after a String.
good point, last time I used this with unmodifyable data I had to either wrapped the data in a callback() or prepended it with a someVar= server side ... the basic proxy was something like echo $HEADERSTUFF; printf "mj=";cat $THEFILE (or wget -O - $THEURL if the file was remote)
0

You make a normal ajax request to the file.json using XMLHttpRequest object. In the onreadystatechange function write your eval logic to receive the json and assign into variable.

It the eval statement would look like

var data = eval ('(' + xhr.responseText + ')');

Go through the following pages if you aren't sure how to work with ajax: http://www.w3schools.com/ajax/default.asp

Edit: Jonathan's answer seems to be a recommended approach; that answer didn't come to my mind at the time. Use JSON.parse() instead of eval().

var data = JSON.parse(xhr.responseText);

1 Comment

This seems like one of those examples that contributed to w3fools. eval() isn't necessary for this. Use JSON.parse().
0

Well, the easiest thing that crossed my mind is to create an iframe with the source to json and on the load event access it's content with javascript to assign in a variable, or implement by hand what a common JSFramework would do...

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.