var obj = JSON.parse(string);
Where string is your json string.
Videos
In order to better understand how JSON.stringify works, I've been tasked with creating a function that can take either an array or an object literal and convert either one into a string. For example:
[10000, "kani"] --> " [10000,"kani"] "
{ a: "is for apple" } --> " {"a":"is for apple"} "
{ foo: true, bar: false, baz: null } --> "{"foo":true,"bar":false,"baz": null}"Also, the object keys need to be in quotation marks too. I've been wracking my brain trying think of a strategy and cannot come up with anything. I've tried using the reduce method, and was able to surround the keys with quotations using template literal syntax for some test cases but not all. And I can't figure out how to set the entire object, which is my accumulator, to either an object string '{ }' or an array string '[ ]' and still be able to update the value of accumulator as reduce iterates. Here's the code I've tried:
function stringMaker(input) {
input = Object.entries(input).reduce((accum, element) => {
let [key, value] = element;
accum["${key}"] = value;
return accum;
}, { } );
return input;
}
const object1 = {a: 'is for apple'};
console.log(stringMaker(object1)) --> {"a": 'is for apple'} //no quotation marks
const object2 = { foo: true, bar: false, baz: null }
console.log(stringMaker(object2)) --> {"foo": true, "bar": false, "baz": null} //no quotation marks
const object3 = {a:{"b":"c"}}
console.log(stringMaker(object3)) --> Object({ "a": Object({ b: 'c' }) }) --> expected '{"a":{"b":"c"}}'
//I have no idea what happened hereAs you can see my results are all over the place. I don't even know why the last case is returning Object. And I haven't even started testing with arrays yet. Advice/hints please!
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));
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.