Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JS object

{
    aString:[aNumber, aURL]
}

JSON.stringify() returns

{
    "aString":"[number, \"aURL\"]"
}

I thought that valid JSON can have arrays as values. Can I have stringify return the JSON string without converting the array into a string? Basically I need turn the whole JS object straight into a string, without any modification.

Is there a better way to do this? I've been looking around but everyone suggests using JSON.stringify if I want an object to string, and no one has raised this problem.

EDIT: Thanks for the quick responses. Here is how I created my JS object, please let me know if I messed up and how!

cookie = {};
// productURL is a string, timer is a number, imageSrc is a URL string
cookie[productURL] = [timer, imageSrc];
// then, I just stringified cookie
newCookie = JSON.stringify(cookie);

If it is also relevant, I am setting an actual cookie's value as the resulting JSON string, in order to access it in another set of functions. Setting the cookie's value does do some URI encoding of its own, but I've actually been grabbing the value of newCookie in the Chrome console as well and it also returns the Array as a string.

share|improve this question
2  
It sounds like [aNumber, aURL] was already a JSON string, so you're stringifying it twice. –  Barmar Apr 14 at 21:51
    
Show how you created the JS object. –  Barmar Apr 14 at 21:51
    
I've added some notes in my edit. Thank you! –  user4694769 Apr 14 at 22:06

3 Answers 3

up vote 2 down vote accepted

If an object you're trying to stringify has a toJSON function, that will be called by JSON.stringify. Most likely you have an external library that's adding Array.prototype.toJSON.

For example, an old version (1.6) of Prototype JS will "conveniently" add that for you.

Prototype 1.6.1:

alert(JSON.stringify([1, 2, 3]));
<script src="http://prototypejs.org/assets/2009/8/31/prototype.js"></script>

Whereas a newer version will not.

Prototype 1.7.2:

alert(JSON.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.2/prototype.js"></script>

You could try deleting Array.prototype.toJSON just to see if that's what's causing the problem. If it is, you might want to look into upgrading/deprecating any libraries in your code that do weird things like that.

Prototype 1.6.1 (after deleting toJSON)

delete Array.prototype.toJSON;
alert(JSON.stringify([1, 2, 3]));
<script src="http://prototypejs.org/assets/2009/8/31/prototype.js"></script>

share|improve this answer
    
This is perfect! Thank you. I've tested your hypothesis and it is indeed true. I don't have 100% control or knowledge over the source code, as it is a client's site, so I had no idea. Now I know :) I will tell them to upgrade their prototype.js library. –  user4694769 Apr 15 at 14:03
    
Keep in mind that the last update to prototype.js was about a year ago, and the last update before about two years before that. You can see the revision history here: prototypejs.org/download. There seem to be a few recent commits on their github page, but it might be worth looking into another library that is more actively developed unless the code heavily depends on Prototype. –  redbmk Apr 15 at 20:52
    
Thank you for your suggestion! I will suggest that to the client, as prototype.js may cause future headaches for them and their partners. At the end of the day it will be their decision though... I am not building their site, just implementing an ad platform. –  user4694769 Apr 16 at 13:53

Based on your description this is not what should happen.

If you have code like this:

var obj = {
    aString:[123, "test"]
}

document.getElementById("out").value = JSON.stringify(obj);

it will generate the expected json:

{"aString":[123,"test"]}

also see https://jsfiddle.net/zudrrc13/

in order to produce your output the original object would have to look something like:

var obj = {
    aString:"[123, \"test\"]"
}
share|improve this answer

Here is the result from pasting your JSON object

{
aString:[aNumber, aURL]
}

into jsonlint.com

Parse error on line 1:
{    aString: [        a
-----^
Expecting 'STRING', '}'

It doesn't look like it is valid JSON to start with. You are right. JSON can have arrays as values but as far as I am aware the labels must be wrapped in quotes.

{
    "aString": [
        "aNumber",
        "aURL"
    ]
}

reurns "Valid JSON"

as also does

{
"aString": [
    "aNumber",
    4
    ]
}
share|improve this answer
    
OP said that was a JS object which should be converted to JSON. It didn't start out as JSON. –  redbmk Apr 14 at 22:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.