0

I want to receive my javascript object such as

{ "user": { "active": true,  "dob": '1988-09-11', "group": 14, "department: "business"" } }

from input using a node package such as readline-sync and store it as an object to be able to access to the values seperately using their keys. The readline-sync stores my input as an string I tried to convert it to javascript object using

let obj= JSON.parse(JSON.stringify(stringObj));

but still it is string. Is it possible to receive a string from the input and convert it object?

falinsky
  • 7,229
  • 3
  • 32
  • 56
ethertest
  • 317
  • 4
  • 13

1 Answers1

1

JSON.stringify() Receives an object and returns a string. If what you want to do is the inverse (convert a JSON string into an object), you need to use JSON.parse() Then, what you want is:

let obj = JSON.parse(stringObj);
JohnL
  • 105
  • 1
  • 3
  • 12
  • Then it gives this error. Unexpected token ' in JSON at position 36 or Unexpected number in JSON at position. The values of my Object could be date or numbers and all of them are not string. – ethertest Jul 16 '19 at 18:57
  • Strings on JSON (such as the "dob" property on your question) notations have double quotes ("). Also, you have missing quotes on the "department" property. Your JSON string should look like this: `'{ "user": { "active": true, "dob": "1988-09-11", "group": 14, "department": "business" } }'` – JohnL Jul 16 '19 at 19:02