0

I have changed data in string format where it was like [object object] but I want to change the string object into json object I tried json.parse but it not changing into json object

can you please suggest me where I am doing wrong and how to fix this

try {
  var timekeep = await Orders.findAndCountAll({
    where: {
      cid: orders_info.cid,
    },
    order: [
      ['id', 'DESC']
    ],
    limit: 1,
    raw: true,
  });
  var cont1 = JSON.stringify(timekeep.rows[0]);
  var obj = JSON.parse(cont1);
} catch (err) {
  console.log(err)
}

console.log('org data' + timekeep)
console.log('data as string' + cont1);

// now when I am trying to print
console.log('data as json' + obj);

the output of the console.logs

org data [object Object]

data as sttring{"id":4006,"mid":1,"cid":41,"wid":7138,"oid":null,"status":null,"options":null,"starttime":"2018-08-15T06:08:55.000Z","duration":null,"ordertotal":50,"counter":null,"closetime":null}

data as json [object object]
8
  • 2
    var obj = JSON.parse(obj); => var obj = JSON.parse(cont1); Commented Apr 8, 2019 at 11:21
  • FYI, objects are objects. They can be represented as JSON, which is a string. So saying "string object" and "JSON object" makes no sense. Object or JSON - that's it. Commented Apr 8, 2019 at 11:23
  • 1
    Also, change the last line to console.log(obj); - It's already an object. When you do console.log("something" + object) it automatically turns the object into a string and appends it to the string you're logging. If you want to log with a bit of text to tell you what you've logged then use a comma like this... console.log("data as object", obj); Commented Apr 8, 2019 at 11:24
  • Just console.log(obj) it's already a json object. Commented Apr 8, 2019 at 11:29
  • @MoadEnnagi There's no such thing as a json object. Commented Apr 8, 2019 at 11:30

3 Answers 3

2

From what I can see you are already converting it to a JSON with var obj = JSON.parse(cont1);

So you already have a JSON, it's just that how you're printing it is wrong. To it with a comma instead of +.

console.log('data as json', obj)

The + is doing a string concatenation, and it's attempting to concatenate a string with an object

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

Comments

0

After String concat it prints data as json [object object]. if you put , instead of + will print that object correctly. In the snippet, you can see the difference.

var jsonstr = '{"id":4006,"mid":1,"cid":41,"wid":7138,"oid":null,"status":null,"options":null,"starttime":"2018-08-15T06:08:55.000Z","duration":null,"ordertotal":50,"counter":null,"closetime":null}';

console.log(JSON.parse(jsonstr));

console.log('data as json' , JSON.parse(jsonstr));

console.log('data as json' + JSON.parse(jsonstr));

Comments

0

console.log just the object; if you want log object and a string use , instead of +

jsonString = '{"key1":"value1","key2":"value2"}'

jsonObject = JSON.parse(jsonString)

console.log(jsonObject) // logging just the object

console.log('jsonObjectName' , jsonObject) // logging object with string 

console.log('jsonObject.key1 : ' +  jsonObject.key1 ) 


// this may come handy with certain IE versions
function parseJSON(resp){
    if (typeof resp === 'string') 
        resp = JSON.parse(resp);
    else
        resp = eval(resp);
    return resp;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.