-1

How to remove double quote from the key parts of the following JSON format i.e "id", "label" and "score" are keys under double quotes that needs to be removed

nodes: [ { "id": "n1", "label": "Node 1", "score": 1.0 },{ "id": "n2", "label": "Node 2", "score": 2.2 },{ "id": "n3", "label": "Node 3", "score": 3.5 } ]

and convert it into the following JavaScript format so that I can plot the graph with CytoscapeWeb

nodes: [ { id: "n1", label: "Node 1", score: 1.0 },{ id: "n2", label: "Node 2", score: 2.2 },{ id: "n3", label: "Node 3", score: 3.5 } ]
Sirko
  • 72,589
  • 19
  • 149
  • 183
Kriss
  • 11
  • 1
  • 1
  • 1
    JS Object with double quoted key works in Javascript – bugwheels94 Apr 18 '15 at 11:56
  • Why do you need to? JSON keys are always quoted, per the spec. You shouldn't be evaluating JSON data as JS code anyway (use json.parse) but if you do, all valid JSON is valid JS anyway. Quotes around keys are optional in JS for convenience, that's all. – IMSoP Apr 18 '15 at 11:58
  • Because Cytoscape Web does not accept it Ankit/IMSoP – Kriss Apr 18 '15 at 11:58
  • @Kriss Any error message etc? – Sirko Apr 18 '15 at 11:59
  • There is no error message. It's just that graph wont' be displaying if i use the first sample code. But with the second sample code it works. Cytoscape web does not accept the first sample code format. – Kriss Apr 18 '15 at 12:05
  • Could you show us a bit more context of how you are using this data? Maybe you're passing the lib the string containing the JSON notation, but it's expecting the actual object? As mentioned, use [JSON.parse](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) to turn the string into actual data. – IMSoP Apr 18 '15 at 12:07
  • Thank you all of you for your quick responses... I used JSON.parse as suggested by IMSoP.. It works now. – Kriss Apr 21 '15 at 06:33
  • Here you found your solution https://stackoverflow.com/a/3651373/17111244 – divyraj Feb 16 '22 at 10:24

2 Answers2

0

I used JSON.parse as suggested by IMSoP.. It works now.

Kriss
  • 11
  • 1
  • 1
0

After looping:

console.log(JSON.parse(JSON.stringify(obj)))
James Risner
  • 5,451
  • 11
  • 25
  • 47
Goodlife
  • 3,822
  • 2
  • 24
  • 23