1

I am getting this error message:

SyntaxError: JSON.parse: expected ',' or ']' after array element at line 4 column 18 of the JSON data.

I think it is because I have an object within an array, and maybe that is not allowed. But I need confirmation of this from experienced developers. Can you put objects in arrays in json?

HTML

<div id="ex1"><h2>Example 1</h2><p></p><h4>results:</h4></div>

Javascript

var message;

  (function loadAjax()
    { var request;
        if (window.XMLHttpRequest)
            {
                request = new XMLHttpRequest();
            }
     else{

         request = new ActiveXObject('Microsoft,XMLHTTP');
     }

     request.open('GET','human.json');

     request.onreadystatechange = function()
     {
         if(request.readyState == 4 && request.status == 200)
             {

                 message = request.responseText;
                 var obj = JSON.parse(message);
                 var text = obj.job[0].hospital;
                 attach('ex1',text,'p');
             }
     }

     request.send();

    })(); 

JSON

{
  "sex":{"male":{"fname":["Michael","Tom"]} ,"female":{"fname":["Alice","Katie"]}},
  "age":[16,80],
  "job":["medical":{"hospital": "doctor"}, "education":{"school":"teacher"} ]
}
7
  • Your job property contains invalid assotiative-like array with object-like keys such as medical and education. An example of a valid array is the property with the age key. The job property should probably contain an object instead of array. Commented May 4, 2016 at 16:33
  • 2
    You just posted this elsewhere. Why not keep them in the same question? stackoverflow.com/questions/37017756/… Commented May 4, 2016 at 16:34
  • @Wes Foster, this is actually different question concerning an array that I have in json. But, I didn't know you can continue to add on to a previous question that you have made Commented May 4, 2016 at 16:36
  • This isn't valid JSON. Are you building it manually? Commented May 4, 2016 at 16:37
  • @EatPeanutButter Yes, I am. Is there a better way to build it? Commented May 4, 2016 at 16:37

3 Answers 3

3

This is incorrect:

"job":["medical":{"hospital": "doctor"}, "education":{"school":"teacher"} ]

This is correct:

"job": {"medical": {"hospital": "doctor"}, "education": {"school": "teacher"} }

Notice I changed the [] to {}

Square brackets are used for arrays (many individual items). Example:

{ "my_array": [1, 2, "three"] }      <-- Notice that the items in square brackets
                                         so not assign a value such as "key": "value"

Curlies are used for associative entries (many "key": "value" items). Example:

{ "my_association": { "dogs": "rule", "cats": "drool" } }

Please refer to JSON.org to learn more about the proper syntax for JSON.

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

3 Comments

Quick question, are you allowed to do the array that I had in objects or is it bad syntax in JSON and javascript objects?
Both are technically the same, they are a Hash. The big different is that with associative entries, you are assigning a name to the key. With normal arrays (in square brackets), you are not assigning a name to the key.
"job":[{"medical":{"hospital":"doctor"}},{"education":{"school":"teacher"}}] is what the op was originally after, but he simply forgot to wrap them key-value pairs in {}.
0

Your job property contains an invalid associative-like array with object-like keys such as medical and education. An example of a valid array is the property with the age key. The job property should probably contain an object instead of array.

JavaScript array is just a list of values (["foo", "bar"]) without explicit keys. If you need a key:value pairs, use an object ({"a" : "foo", "b" : "bar"}).

By the way, it generally makes sense to generate JSON automatically (not manually), e. g. using PHP’s built-in function json_encode(), so that the resulting JSON code is guaranteedly valid.

Comments

0

You will need to correct your:

{
  "sex":{"male":{"fname":["Michael","Tom"]} ,"female":{"fname":["Alice","Katie"]}},
  "age":[16,80],
  "job":["medical":{"hospital": "doctor"}, "education":{"school":"teacher"} ]
}

to:

{
  "sex":{"male":{"mname":["Michael","Tom"]},"female":{"fname":["Alice","Katie"]}},
  "age":[16,80],
  "job":[{"medical":{"hospital":"doctor"}},{"education":{"school":"teacher"}}]
}

in case you really want to preserve your original idea of structure & access.

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.