-2

I'm trying to transform my object person to a JSON.

const person = new Object();
    person.firstName = 'testFirstName';
    person.lastName = 'testLastName';

var myJson = JSON.stringify(person);
                        
console.log(myJson);
console.log(typeof myJson); // This returns me a string, not a JSON.

But when I try to print my supposed JSON and print the type of my element (myJson) I get is an string.

Someone can help me to understand this and can tell me how I could do this?

Caín
  • 181
  • 1
  • 10
  • 4
    You've converted it into a string with `JSON.stringify`. What is so strange here? – Wais Kamal Jun 18 '21 at 08:40
  • 2
    Stringified object IS a JSON `string` – Roberto Zvjerković Jun 18 '21 at 08:40
  • @WaisKamal If I do a typeof of ```myJson```, the data type, should it appear as ```string``? – Caín Jun 18 '21 at 08:43
  • @RobertoZvjerković What do you mean? ```person``` object is already a JSON? – Caín Jun 18 '21 at 08:43
  • 1
    Yes, because it is actually a string, not an object anymore. – Wais Kamal Jun 18 '21 at 08:43
  • 1
    Try var myJson = JSON.parse(JSON.stringify(person)); – ümit Altuntaş Jun 18 '21 at 08:44
  • 3
    `stringify` function is pretty self-explanatory. It creates a `string`. – Roberto Zvjerković Jun 18 '21 at 08:44
  • 1
    @umitAltuntas That is useless, `var myJson = person` will do the same thing –  Jun 18 '21 at 08:45
  • Okay, thank you @WaisKamal and others. So now, how can I print my JSON to check the structure? I'm using datatables library (to create tables) and I need to check if the content is exactly the same structure that expects the library (same JSON structure). If I ty with ```console.log(myJson);``` I just get a string, not in JSON structure. Do you know what I mean? Thank you in advance! – Caín Jun 18 '21 at 08:50
  • 1
    @Caín Log the object before you change it to JSON. That should typically show you the structure. – JLRishe Jun 18 '21 at 08:51
  • 1
    @Caín you can parse it and check. `JSON.parse(yourStringifiedJSON)` – Karma Blackshaw Jun 18 '21 at 08:51
  • @KarmaBlackshaw Yes, is what @Umit Altuntas said me, but I don't understand i. If I already have my JSON doing ```var myJson = JSON.stringify(person);```,why I need to parse it to JSON again? Thank you in advance! – Caín Jun 18 '21 at 08:53
  • 1
    If you already have the parsed object, then there would be no need to parse the JSON. Instead, just log the `person` object. `myJson` and `person` would have the same structure, it's just that `myJson` is string because it has been stringified which is valid JSON. – Karma Blackshaw Jun 18 '21 at 09:01

1 Answers1

1

First you must know what are the differences between JSON & JS object. It is described in this answer link.

Normally when you fetch a JSON file it is in the string format, to use it as a object you have to parse the JSON file and then the string will be converted to object.
In your code by using the JSON.stringify(person) you already have converted the object into a JSON file.