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));
Answer from Andris on Stack OverflowAll 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.
Why convert json to javascript object? Why not use it as is?
Converting a .json to .js
Extension to convert js object to JSON?
How do I convert normal JSON to GeoJSON?
GeoJSON is just JSON, but with a specific schema that is understood by many GIS programs. Without knowing more about your particular data it's not easy to recommend a way to convert it.
Generically, learn the GeoJSON spec and enough Node.js (any programming language works, but JavaScript and JSON go together like peanut butter and jelly), to convert the data. It doesn't have to be pretty elegant code, just enough to get the job done.
More on reddit.comVideos
JSON.parse(jsonString) is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.
The jQuery method is now deprecated. Use this method instead:
let jsonObject = JSON.parse(jsonString);
Original answer using deprecated jQuery functionality:
If you're using jQuery just use:
jQuery.parseJSON( jsonString );
It's exactly what you're looking for (see the jQuery documentation).
Teaching my self how to do JSON with javascript. When I initally have data and the file is like user.json, should I just convert it into .js . What is the most common thing to do? I have been fetching the data and changing it into .js file
Right now I am teaching myself how to fetch local data and data from a API, grab some of it and display it on a html page. I am also a bit confuse whether everything starts with async function await and thens but it seems to be repetitive or at least the way I been doing it.