3

I have observed that in php you can encode an array and the resulting json does not carry the square brackets.However,in my javascript array,

var arrCars = new Array("Toyota", "Mercedes", "BMW");           
        var jsonStr = JSON.stringify(arrCars);
        alert(jsonStr);

i keep getting the square brackets.I have also noticed that if i use json stringfy,

var foo = {};  
foo.bar = "new property";  
foo.baz = 3;  

var JSONfoo = JSON.stringify(foo);

i get the json without the square just like i wanted.What must i do to my array to do away with the brackets?.

7
  • What do you mean? echo json_encode(array("item1", "item2", "item3")); gives me the expected ["item1","item2","item3"]. Also, why would you want to get rid of the brackets? They are a crucial part of JSON. Commented Jun 24, 2012 at 19:44
  • The example i looked at did not produce the brackets.Its php.net/manual/en/function.json-encode.php Commented Jun 24, 2012 at 19:50
  • 1
    That won't produce a javascript array, it will produce an object. If you use an associative array in PHP, you'll get that result. Commented Jun 24, 2012 at 19:51
  • Can any array in javascript produce that result?. Commented Jun 24, 2012 at 19:52
  • 1
    If the brackets are a problem,get rid of them yourself. JSON.stringify(arrCars).replace(/[\[\]]/g,'') you should probably be more careful/selective about how you do that, but just as a simple example Commented Jun 24, 2012 at 19:57

3 Answers 3

2

There's a difference between an array ([]) and an object ({}) in javascript. Your first example uses an array. Your second example uses an object. The main difference is that when you have an array ([]) the index can only be a zero based integer. Whereas an object can have property names that are strings.

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

3 Comments

The difference is implementation optimizations, nice prototype and some magic with numeric "properties" and .length. Array is an object can very well have normal properties.
@Esailija, I agree. But it's important to make the difference between the two and use the proper type based on the requirements.
Sure, just wanted to add a specific side note ;P
1

The correct JSON for the array in your first example is ["Toyota","Mercedes","BMW"], and the correct JSON for the object in your second example is either {"bar":"new property","baz":3} or {"baz":3,"bar":"new property"}.

A JSON string contains either an object or an array, so it always has either curly brackets {} or square brackets [].

If you get anything else, the result is not correct. If you expect anything else, your expectation is wrong.

If you want the curly brackets instead of the square brackets, you have to serialise an object, not an array.

Comments

1

Not sure why you don't want brackets, since brackets are the normal way to make an array literal (as opposed to using the Array() function, as you do, which is way old-school)

If you want the JSON without the brackets, such as if you are building a bigger JSON string or something, you can use something like this:

function jsonifyArrayWithoutBrackets(a) {
    var output = [];
    for (var i=0; i<a.length; i++)
        output.push(JSON.stringify(a[i]));
    return output.join(',');
}

Or, obviously, just trim the brackets off after using a single call to JSON.stringify().

1 Comment

How would i JSON.stringify a javascript associate array?.

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.