13

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.

5
  • 6
    JSON.stringify(cheese_array) will indeed stringify the whole thing; how are you determining that it's not? Commented Jul 8, 2016 at 5:37
  • That is not a valid array Commented Jul 8, 2016 at 5:38
  • 1
    There are useful examples here : stackoverflow.com/questions/6487699/… Commented Jul 8, 2016 at 5: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. Commented Jul 8, 2016 at 7: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. Commented Jul 8, 2016 at 7:12

4 Answers 4

10

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.

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

12 Comments

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.
Comma was not the issue.. Remove it and try..That will throw Syntax Error
@Rayon Remove what the comma? if you remove the comma its not a valid array. Obviously in that case its a syntax error.
@Rayon thanks updated I appreciate your hard work in convincing so many people who have answered practically the same thing :).
@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. :(
|
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>

Comments

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.

8 Comments

Comma was not the issue.. Remove it and try..That will throw syntaxError
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.
OP is claiming [object Object], [object Object], [object Object] to be the output he got.. Did comma make this happen ?
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 :)
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.
|
-3

I was able to fixed this by calling

JSON.stringify(JSON.stringify(objToStringify))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.