-7

I am trying to convert the following Javascript object into valid JSON:

[{ 
 '"Sno"': '"1"',
 '"Purchase Date Time"': '"2017-11-14 14:09:32"',
 '"Txn Type"': '"COD"',
 '"Order Status"': '"DELIVERED"'
},
 { 
 '"Sno"': '"2"',
 '"Purchase Date Time"': '"2017-11-14 14:09:32"',
 '"Txn Type"': '"COD"',
 '"Order Status"': '"DELIVERED"'
}]

I have already tried JSON.stringify() but it gives result as:

"[{"\"Sno\"":"\"1\"","\"Purchase Date Time\"":"\"2017-11-10 14:09:32\"","\"Txn Type\"":"\"COD\"","\"Order Status\"":"\"DELIVERED\"",}]"

This way I am unable to access the individual elements. I want access the value like objectName[i].Sno.

How can I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shashi Kumar Raja
  • 508
  • 1
  • 8
  • 23
  • How about `JSON.stringify(yourObject)` ? – 3Dos Nov 15 '17 at 13:18
  • Please provide the code you've already tried, and we'll help you get it working. – Rob Johansen Nov 15 '17 at 13:19
  • JSON.stringify is not working because I need to get rid of '" – Shashi Kumar Raja Nov 15 '17 at 13:19
  • 1
    just read the documentation for JSON.stringify and you will find your answer – Robbie Milejczak Nov 15 '17 at 13:22
  • `JSON.stringify` works perfectly. It's your data that's already messed up. Why do all your values already contain quotes?! – deceze Nov 15 '17 at 13:26
  • @deceze that is how I am getting it. How can I convert it into a valid usable json – Shashi Kumar Raja Nov 15 '17 at 13:28
  • The JSON you're producing is already *valid.* What you want is to *strip the quotes from your data.* Tried anything in that regard? Maybe you could fix the data at the source, wherever you're getting it from? – deceze Nov 15 '17 at 13:30
  • 1
    *"I want access the value like objectName[i].Sno"* Then you don't want JSON at all. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Nov 15 '17 at 13:30
  • @T.J.Crowder Instead of replace it makes sense to use JSON.parse to get rid of extra quotes. – dfsq Nov 15 '17 at 13:39
  • So if you don't want JSON (see my comment above), and you can't fix this at the source (which is the right thing to do), you'll want to create new objects with the quotes removed from the keys and values. That'll involve `Array#map`, `String#replace`, and probably `Object.keys`. More [on MDN](https://developer.mozilla.org/en-US/docs/JavaScript/). – T.J. Crowder Nov 15 '17 at 13:39
  • @dfsq: On the individual strings (keys and values)? Hmmm, yeah, that could indeed make sense. If the reason they have quotes is something has double-encoded them... – T.J. Crowder Nov 15 '17 at 13:40

1 Answers1

0

If you are still interested you can do this:

var js = [{ 
 '"Sno"': '"1"',
 '"Purchase Date Time"': '"2017-11-14 14:09:32"',
 '"Txn Type"': '"COD"',
 '"Order Status"': '"DELIVERED"'
},
 { 
 '"Sno"': '"2"',
 '"Purchase Date Time"': '"2017-11-14 14:09:32"',
 '"Txn Type"': '"COD"',
 '"Order Status"': '"DELIVERED"'
}];

var json = JSON.parse(JSON.stringify(js).replace(/\\\"/g, ""));

console.log(json[0]["Order Status"]);

Convert to string, remove all \" pairs, then convert back to JSON.

U Rogel
  • 1,893
  • 15
  • 30