-4

I've searched the internet for an answer and what I found was that JSON.stringify() cannot convert for example functions, only pure data.

But this answer couldn't fulfill my need.

I need to save the json I requested via fetch in a string because of how AsyncStorage works So I take the jsonContent and save it as a stringified version (jsonContent is the fetched json)

var temp = JSON.stringify(jsonContent);

Then later I need to recall it so I return it back to it's json format, but it does not accept the output as a json output.

var output = JSON.parse(temp);

The json I wanna fetch is:

{
    "content": [{
        "name": "this",
        "desc": "is",
        "explanation": "just a",
        "time": "test",
        "class": "with one",
        "image": "item",
        "id": "0",
        "l_name": "testing"
    }]
}

The fetched version of jsonContent (This is useable, pre-stringify&parse):

Object {
   "content": Array [
     Object {
       "class": "with one",
       "desc": "is",
       "explanation": "just a",
       "id": "0",
       "image": "item",
       "l_name": "testing",
       "name": "this",
       "time": "test",
     },
   ],
 }

The fetched version of jsonContent (This is not useable, post-stringify&parse):

Object {
   "content": Array [
     Object {
       "class": "with one",
       "desc": "is",
       "explanation": "just a",
       "id": "0",
       "image": "item",
       "l_name": "testing",
       "name": "this",
       "time": "test",
     },
   ],
 }

Original code:

How do i fix my json so I can use JSON.stringify(JSON.parse(jsonContent))

If you need anymore information, just ask. Thank you for your time.

NoahS
  • 21
  • 1
  • 6
  • Possible duplicate of [Javascript: Why are two objects not equal?](https://stackoverflow.com/questions/33299889/javascript-why-are-two-objects-not-equal) – Jared Smith Dec 08 '18 at 16:06
  • They are identical in terms of usage, but they are two different objects with exactly the same properties and the equality operators (==, ===) will only return true if they are the same object. Don't confuse what console.log shows with what your actual object is. – Jared Smith Dec 08 '18 at 16:09

2 Answers2

0

That is because objects and arrays in javascript are evaluated with there references. When you are doing JSON.parse(JSON.stringify(responseJson)) a new array is created with new reference. And when JavaScript Engine tries to evaluate the object with there reference he encounters different references and that's why it returns false.

If you want to check two objects, Then you have to iterate over each value of object using loops.

-2

I will just reply according to your question. JSON.stringify converts to json. Then Json.parse gives an array. So, that array is not equal to your json

rakesh shrestha
  • 1,335
  • 19
  • 37
  • That is not at all accurate. – Jared Smith Dec 08 '18 at 16:06
  • @ReyYoung `JSON.parse` does not give you an array unless you give it one, which is not the case here. The reason the two aren't equal is because JavaScript compares non-primitives on reference equality rather than value equality. The reason the parsed version looks different is just `console.log` formatting. This "answer" mentions neither and contains a factual inaccuracy besides. – Jared Smith Dec 08 '18 at 18:06