var obj = JSON.parse(string);
Where string is your json string.
javascript - Converting a string to JSON object - Stack Overflow
javascript - Convert JS object to JSON string - Stack Overflow
Converting a string to JSON javascript
creating an object string without using JSON.stringify
All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:
var j = {
"name": "binchen"
};
console.log(JSON.stringify(j));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
With JSON.stringify() found in json2.js or native in most modern browsers.
JSON.stringify(value, replacer, space) value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. space an optional parameter that specifies the indentation of nested structures. If it is omitted, the text will be packed without extra whitespace. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as "\t" or " "), it contains the characters used to indent at each level.
I know this is simple with `JSON.parse()` but that will not seem to work in my case. I think the issue is that `JSON.parse()` works only when the object is in the format of `{"key": "value"}` not in a `{key: "value"}` like I have. Below is a copy of the string that needs to be converted to JSON along with the error message.
{ ignoreSSLError: false,
username: null,
_id: 5c7abe63087bd40ff8439fec,
createdAt: 2019-03-02T17:33:23.312Z,
__v: 0 }
SyntaxError: Unexpected token i in JSON at position 2
Suggestions on how I can fix this? Changing the initial string is not an option. That is how it gets pulled fro AWS SQS, I wish it was that easy.