11

I have created an array of objects that needs to be stored and kept for another page.

The array of objects is similar to this:

var cheese_array = [
  {
    name: "Chedder",
    age: "34",
    smelly: true
  },
  {
    name: "Brie",
    age: "4",
    smelly: false
  },
  {
    name: "Blue Stilton",
    age: "13",
    smelly: true
  }
 ];

But when I JSON.stringify() it, it doesn't stringify the objects, only the array. So I end up with and array that looks like this:

[object Object], [object Object], [object Object]

So how do you stringify these objects in this array.


EDIT: This array of objects is then passed to an on click function similar to this:

$("#a-button").click(function() {
  var cheese_arr_stringify = JSON.stringify(cheese_array);
  sessionStorage.cheeseArray = cheese_arr_stringify;
  if(sessionStorage.cheeseArray) {
    window.location.href = "../";
  }
 });

So pretty much, its sets cheese_arr_stringify to a stringified version of the array of objects. Then it sets this stringified code to a session key. Following this, once it has been set cheeseArray it send it up one directory.


EDIT 2: This is an image of the session key after being stringified. In this case, foodItems is the same as cheeseArray

Session Storage


EDIT 3: @Rayon asked for a fiddle so he could have a look, I made it up and it had worked. The problem was - I feel so stupid now - that I was calling the array instead of the stringified var I had made.
sparcut
  • 795
  • 1
  • 10
  • 27
  • 6
    `JSON.stringify(cheese_array)` will indeed stringify the whole thing; how are you determining that it's not? – Jacob Jul 08 '16 at 05:37
  • That is not a valid `array` – Rayon Jul 08 '16 at 05:38
  • 1
    There are useful examples here : http://stackoverflow.com/questions/6487699/best-way-to-serialize-unserialize-objects-in-javascript – Ugo T. Jul 08 '16 at 05:39
  • Can you let us know how you are running this code? For example in Chrome console or Node. The display you are showing is similar to how Chrome and Firefox display a variable if you enter in the console. This is what I get when entering the variable name and hitting enter on the chrome console. [>Object, >Object, >Object] The greater thans are actually triangle buttons in Chrome. – Philip Tinney Jul 08 '16 at 07:02
  • @PhilipT. Yes, I am using chrome. I am viewing the session stores keys which is where the stringified array is being pushed after being stringified. I will notify and show you the function that performs the stringify and pushes to session storage. **EDIT**: Also, the objects are not displayed with triangles, I know what you mean, but session storage can only store strings, I will add image of session stored key. – sparcut Jul 08 '16 at 07:12

4 Answers4

9

Your object misses a comma as shown below:

name: "Blue Stilton",
    age: "13"//comma is missing here
    smelly: true

JSON.stringify works fine as shown below.

var cheese_array = [
  {
    name: "Chedder",
    age: "34",
    smelly: true
  },
  {
    name: "Brie",
    age: "4",
    smelly: false
  },
  {
    name: "Blue Stilton",
    age: "13",
    smelly: true
  }
 ];
console.log(JSON.stringify(cheese_array))

However I am not sure how you get a log [object Object], [object Object], [object Object] I am presuming you are console logging something else please check that in your code.

Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
  • Woops, sorry missed that. That is what I have been doing, but it seems to be only stringify-ing the array, but not the objects inside. – sparcut Jul 08 '16 at 05:43
  • `Comma` was not the issue.. Remove it and try..That will throw `Syntax Error` – Rayon Jul 08 '16 at 05:47
  • @Rayon Remove what the comma? if you remove the comma its not a valid array. Obviously in that case its a syntax error. – Cyril Cherian Jul 08 '16 at 05:50
  • @Cyril – OP is claiming `[object Object], [object Object], [object Object]` to be the output he got.. Did _comma_ make this happen ? – Rayon Jul 08 '16 at 05:50
  • hmm..I agree to that but up i have put a working code. May be he is console logging something else. – Cyril Cherian Jul 08 '16 at 05:52
  • @Cyril – How about highlighting that in your answer.. Will be more appropriate in provided _context_ – Rayon Jul 08 '16 at 05:55
  • @Cyril – Also note _"Your json misses"_.. __NO__, It is an `object`, not `JSON` – Rayon Jul 08 '16 at 05:57
  • 3
    @Rayon thanks updated I appreciate your hard work in convincing so many people who have answered practically the same thing :). – Cyril Cherian Jul 08 '16 at 06:01
  • @Cyril I am not console logging the array, the stringify we array was pushed to a session key and came up as the aforementioned array that said object Object. I will update main post with image when I get home – sparcut Jul 08 '16 at 09:22
  • 1
    @Rayon I feel so stupid. I made a fiddle, was surprised that it worked. Went and stared at my code for a couple of minutes. I had been calling the array instead of the stringified var. Sorry for wasting your time. :( – sparcut Jul 09 '16 at 03:48
  • @sparcut I knew this was going to happen.. That's the reason I asked for a fiddle.... – Rayon Jul 09 '16 at 04:11
  • My advice: `JSON.stringify(cheese_array:cheese_array);` – Bruno L. Aug 06 '20 at 15:07
4

Update JSON with ,(Comma) in

 name: "Blue Stilton",
    age: "13",
    smelly: true

var cheese_array = [
  {
    name: "Chedder",
    age: "34",
    smelly: true
  },
  {
    name: "Brie",
    age: "4",
    smelly: false
  },
  {
    name: "Blue Stilton",
    age: "13",
    smelly: true
  }
 ];

var details = JSON.stringify(cheese_array);
alert(details);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
1

You're missing a , in your 3rd object, after age:

var cheese_array = [
  {
    name: "Chedder",
    age: "34",
    smelly: true
  },
  {
    name: "Brie",
    age: "4",
    smelly: false
  },
  {
    name: "Blue Stilton",
    age: "13",
    smelly: true
  }
 ];

var jsonObj = JSON.stringify(cheese_array);

This will now work and display correctly.

Wesley Coetzee
  • 4,768
  • 3
  • 27
  • 45
  • `Comma` was not the issue.. Remove it and try..That will throw `syntaxError` – Rayon Jul 08 '16 at 05:47
  • The 'comma' was missing after age inside the 3rd object, that was the syntax error and that's why it was not working, you can see other people ended up providing the same answer. – Wesley Coetzee Jul 08 '16 at 05:49
  • 1
    OP is claiming `[object Object], [object Object], [object Object]` to be the output he got.. Did _comma_ make this happen ? – Rayon Jul 08 '16 at 05:50
  • If you remove the comma, so the 3rd object, becomes 'age: "13"' this IS a syntax error. I've tested the code before I posted it here, that's how I found the error. I think you're misunderstanding the issue. Please look at the OPs code and maybe run it and you'll see :) – Wesley Coetzee Jul 08 '16 at 05:52
  • 1
    IMO, One should provided a solution considering the scenario OP is facing.. OP never said he has an error.. What he said is he is geeing [`object object`] as output.. Your solution is not helping that cause.. This question perfectly fits as __A problem that can no longer be reproduced or a simple typographical error.__ – Rayon Jul 08 '16 at 05:54
  • @Rayon +WesleyCoetzee Sorry, I had written that array up in a rush. The original array of objects I am working with is to large to copy paste, but all commas are where they should be. Not getting any syntax errors, and it works fine when using it on that document. But the problem arises when stringifiying it then storing it to the session. I am using chrome for my testing. I am on my phone atm, but when I get back home, I'll double check the function. – sparcut Jul 08 '16 at 07:09
  • Alright, I have updated with an image of the `[object Object], [object Object], [object Object]` output. – sparcut Jul 08 '16 at 10:38
  • Are you calling he array instead of the `JSON.stringify` variable? – Wesley Coetzee Jul 09 '16 at 18:08
-3

I was able to fixed this by calling

JSON.stringify(JSON.stringify(objToStringify))