0

I have json format string that has keys without inverted commas. So I am not able to parse this string to json using var jsonV = JSON.parse(fileStr). How can I put the keys of dictionary into inverted commas and then parse the string to json? Below is the example string I want to parse into json.

{
  title: 'Service Requests',
  navTitle: 'Service Requests',
  headers: {
    id: 'ID',
    priority: 'Priority',
    status: 'Status',
  },
}

I am getting this as string only. I want to parse this into JSON.

Edit: I tried this and this links. But none of these answered my question. These doesn't take the condition that if we have only string having json elements without inverted commas like in below example. (I cant even iterated through this string object as)

Tanmay Bairagi
  • 564
  • 5
  • 25
  • 2
    Ideally, of course, fix the thing that's giving you this string so it gives you JSON rather than a JavaScript object literal. But if you can't do that, see the answers to the linked question. In short: Hack a JSON parser to make it allow unquoted keys and `'` quoted strings, or use `eval` ***IF*** you can totally trust the source of the string (and **not** if you can't). – T.J. Crowder Mar 23 '22 at 10:15
  • This npm module might be useful, better than using eval anyway.. -> https://www.npmjs.com/package/js-object-parser Oh, forget that link, looked at the code and it's just a regex replacer, I think that's a bit hacky. Something using an AST parser would be nicer. – Keith Mar 23 '22 at 10:17
  • I don’t understand what you mean by _“I tried \[[Parsing string as JSON with single quotes?](/q/36038454/4642212)\]. But none of these answered my question.”_. [`JSON5.parse(`_your string_`)`](/a/49188147/4642212) works perfectly fine. Do you have a [mre] of your attempts that you can [edit] into your question? – Sebastian Simon Mar 24 '22 at 22:47

0 Answers0