JSON.stringify() won’t output Object properties if your variable is an Array

by zack.moore June 25, 2010 23:39

I was recently editing some code that another developer had written when I noticed some strange behavior.

Note: in this post I use the terms “object properties”, “expando properties”, and “dictionary values” interchangeably. In JavaScript, you can add named values to an object in a few ways but each is mostly interchangeable. For example, these do the same thing:

x["a"] = 1;
x.a = 1;

I needed to take an existing JavaScript object and stringify it and send the JSON to the server. That’s fairly straightforward, but

Technorati Tags:

I quickly realized that the JSON I was receiving was missing a bunch of values that I was expecting.

It turned out the source of the error was in the variable declaration of the object I was stringifying. The variable was declared as:

var a = new Array();

But, when values were added to the variable they were added using the dictionary syntax such as:

a[“key”] = 3;

Even though the variable is declared wrong, it appeared to work fine everywhere. That is, until I tried to stringify it.

A quick note, if you don’t know what stringify is then take a look over at json.org. The short answer is that stringify() is a method on the JSON object that lets you turn a JavaScript object into its JSON representation.

The problem seems to be that when stringify() encounters an array object, while it does serialize the indexed elements it doesn’t serialize any expando properties.

This makes sense if you think about it. JSON has a representation for objects with properties and for arrays, but the array notation has no way of also including properties.

The example below demonstrates that no dictionary values that were added to the array are serialized when the array is stringified, but in the object the properties are output. Note, I used the shorthand notation for object {} and array [] in the example below.

Example Code:

<label for="out_a1">A property read: </label><span id="out_a1"></span><br />
<label for="out_a2">A strinify: </label><span id="out_a2"></span><br />
<label for="out_b1">B property read: </label><span id="out_b1"></span><br />
<label for="out_b2">B strinify: </label><span id="out_b2"></span><br />
<script type="text/javascript">
    var a = {};
    var b = [];

    a["alpha"] = "alpha";

    b["beta"] = "beta";

    var out_a1 = document.getElementById("out_a1");
    out_a1.innerHTML = a["alpha"];
    var out_a2 = document.getElementById("out_a2");
    out_a2.innerHTML = JSON.stringify(a);

    var out_b1 = document.getElementById("out_b1");
    out_b1.innerHTML = b["beta"];
    var out_b2 = document.getElementById("out_b2");
    out_b2.innerHTML = JSON.stringify(b);
</script>

 

Example Output:

A property read: alpha
A strinify: {"alpha":"alpha"}
B property read: beta
B strinify: []

The solution in my problem was to change the object declaration from array to object and everything worked fine.

I could have run into a problem if the object was being accessed as both an array and an object. You can do this. I tried it out in a couple of browsers (IE8 and Chrome) and it worked. If that had been the case, then I would have had to refactor the code because there would be not way to output both the array elements and the object properties in one JSON element.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

JavaScript

IE Modal Dialog - Debug Y/N Rant

by zack.moore November 20, 2008 14:24

I first wrote this up as a post on the ASP.NET Client Side Web Development Forum when I realized that my blog is badly neglected and I ought to be writing stuff there. You can find my forum post here to see if anybody responded over there.

Yesterday I was reading the Visual Web Developer Blog. They have a really good article on JavaScript Intellisence in Visual Studio. http://blogs.msdn.com/webdevtools/archive/2008/11/18/jscript-intellisense-faq.aspx

In the article and comments the author was really encouraging users to post their issues with the technology because they really did use it to improve the products.

That request got me to thinking about the different little issues that I run into as a developer and I was reminded of something that I deal with countless times every day. I started writing it up as a comment in that blog post but I stopped because it was a little off topic and I didn't want to pollute the blog comments with off topic stuff.

So I decided to write up my little rant here .

I don't know how many other people this affects but the following issue affects me every day. In order to be able to debug JavaScript in web pages, I leave my browser in "debug" mode all the time because it is a pain to switch it on and off. This allows me to quickly jump into some JavaScript if I find a problem. However, the consequence is that now every page I browse to with IE pops up that little dialog asking me if I want to debug a script error on the page. It is amazing to me how many pages have so many script errors (at least in IE). I also think this causes a lot of my IE crashes especially when I get 2 or more dialogs in deadlock with each other on different tabs. Does anyone else get this?

Every time I go to cnn.com I know before I even type in the address that I'm going to get at least 2 Debug popups.

Something that compounds the problem for me is that I'm a "Right Click -> Open in New Tab" browser. As I read, if I encounter a link that I'm interested in I open it in a new tab and keep reading. After I finish the page I'm on, I go to the next tab and start reading. But this causes conflicts and lockups when multiple tabs popup Modal Debug Dialogs and I think that leads to a lot of the crashes that I get.

But it is annoying even if you don't browse that way. This problem is at its worst if you even encounter a page that causes a LOT of JavaScript errors or if you browse to a page that triggers an error when your mouse hovers over an element that you have to cross in order to get your mouse out of the browser window so you can close it. Every time you try to get the mouse out you trigger a modal dialog. After you close it, you try to get out again and re-trigger the modal dialog.

So how do other people deal with this? Is there a trick that I don't know that would make this more bearable?

How about the IE team? Could that change how this works so that it wouldn't cause so many problems and halts when I'm working? I would love to see a way to turn debugging on and off easier. I would also love to see the modal dialog removed and replaced with something else like a little error icon or something. The work on IE 8 has been awesome so far. the new debug and dev tools are pretty neat. How about making it easier for web developers to browse the web normally when not debugging?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

JavaScript | IE

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant


I've been doing software development since I was little and my dad first brough home an ATARI 800. I picked up PILOT and BASIC. Now I mostly write C# and ASP.NET, and about a dozen other languages and platforms. I also enjoy a bunch of outdoor sports including running and mountain biking. I am very happily married. I am currently working for a great Software and IT consulting company named SPINEN where I am a Senior Developer.


Copyright Zack Moore

TextBox