-3
var obj = '{name: "amit", age: 40}'

I have a object which im getting it in parse, how to parse this object ?

Amit Paple
  • 11
  • 1
  • 7
  • 1
    The string is already stringified. Did you mean parse? Either use valid JSON, or see [Need less restrictive json parser](/q/14686693/4642212) for inferior alternatives. – Sebastian Simon Jan 27 '23 at 04:50
  • @SebastianSimon im getting the json in string format? how we can handle it ? – Amit Paple Jan 27 '23 at 05:16
  • @AmitPaple No, you don’t. This isn’t JSON. Read the linked Q&A posts and the [documentation on what JSON is](//developer.mozilla.org/en/docs/Glossary/JSON). _“how we can handle it ?”_ — [Edit] and clarify your question first. Read [ask]. It’s not clear what exactly you currently have and what exactly you need. – Sebastian Simon Jan 27 '23 at 05:18
  • 1
    JSON string has double quotes around the keys. There are ways to parse it ([Parsing "relaxed" JSON without eval](https://stackoverflow.com/questions/9637517)). But, it's a good idea to fix the source to return the correct JSON./ – adiga Jan 27 '23 at 05:20
  • @AmirSaleem im parsing the json. – Amit Paple Jan 27 '23 at 05:20
  • Also, please clarify exactly what you need. Title says `parse` and the body says *how to stringify the object*. `var obj = { key: "value" }` <- This is an object. `JSON.stringify(obj)` <- This returns the JSON string. JSON.parse('{ "key": "value" }') <- This is parsing a JSON string. – adiga Jan 27 '23 at 05:23
  • @SebastianSimon found the solution `var obj = '{name: "amit", age: 40}'` `var correctJsonValue = obj.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');` `JSON.parse(correctJsonValue);` – Amit Paple Jan 27 '23 at 05:24
  • 1
    @AmitPaple again, the regex solution is a hack and will eventually fail when you have complex structures. Fix the source of the JSON. – adiga Jan 27 '23 at 05:25
  • @adiga Please see the question. key is not in string. only value is in string. and in that case if you parse it then i'm getting error as `Uncaught SyntaxError: Expected property name or '}' in JSON at position 1 at JSON.parse () at :1:6` – Amit Paple Jan 27 '23 at 05:29
  • @AmitPaple `[a-z0-9A-Z_]+` doesn’t cover the full range of valid [_PropertyName_ productions](//tc39.es/ecma262/#prod-PropertyName). The `:` is not required to follow immediately after the _PropertyName_. A space is optional after the `:`. This regex will fail eventually. – Sebastian Simon Jan 27 '23 at 05:30
  • @AmitPaple that's what I meant earlier. Your JSON is invalid because it is missing quotes. Fix where this JSON is created instead of finding ways to parse this malformed JSON. – adiga Jan 27 '23 at 05:32
  • @AmitPaple _“key is not in string. only value is in string.”_ — To be very clear: as presented in your question, `obj` is a String. The String `obj` represents a valid object initializer, but not valid JSON. The object initializer that `obj` represents lacks _quotes_ around the key names. If `obj` had the value `{name: "amit", age: 40}`, it would be an Object. That object’s `name` property would have a String value, and the object’s `age` property would have a Number value. Both property keys are Strings. Given this terminology, what exactly do you mean by “in string”? What do you expect? – Sebastian Simon Jan 27 '23 at 05:36

1 Answers1

-3

Try this -

var strObj = JSON.stringify(obj); console.log(strObj);

sohan
  • 577
  • 1
  • 6
  • 9