I've never worked with any DB or back-end, etc stuff, but I am in need of some sort of data storage. I'm working on a javascript application that will only be run on my pc, offline, and I need to be able to save information. I don't want to rely on localStorage because if the browser history is wiped then all the data goes with it.
While searching for a way to collect and store info, I read about JSON, and it sounded like what I was looking for--and yet I've spent the last 4 hours watching tutorials and so far all I know about it is it's fching JS. I sat through a 12 minute video where all the guy did was write out an object in json and then copy and paste into a js file and said "now you know how to use json in all your future projects" 🙄 like what in ACTUAL fk. You could have just WROTE that in js. What's the point of JSON? Everything I've seen or read is practically just the same as this video.
DOES json collect and store data?
Like, if I put an input form in my app, and type a name and hit submit, can I make that Input hardcode into the json file to be saved forevermore and called upon when I needed in this app? Because that's what I need. Any explanation or help on this would be GREATLY appreciated.
How to check if a string is a valid JSON string?
Can comments be used in JSON? - Stack Overflow
xml - Is JSON a language, if not how would it be classified? - Stack Overflow
Is nested JSON the same as dot-path in VS Code settings.json?
Videos
Here's a function that uses JSON.parse to return a bool indicating whether the JSON string can be successfully parsed:
function isJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
Here's a similar function that will either return the parsed object, or null if it couldn't be parsed.
function parse_json(json_string)
{
let json_object = null;
try
{
json_object = JSON.parse(json_string);
}
catch (e)
{
return null;
}
return json_object;
}
I know i'm 3 years late to this question, but I felt like chiming in.
While Gumbo's solution works great, it doesn't handle a few cases where no exception is raised for JSON.parse({something that isn't JSON})
I also prefer to return the parsed JSON at the same time, so the calling code doesn't have to call JSON.parse(jsonString) a second time.
This seems to work well for my needs:
/**
* If you don't care about primitives and only objects then this function
* is for you, otherwise look elsewhere.
* This function will return `false` for any valid json primitive.
* EG, 'true' -> false
* '123' -> false
* 'null' -> false
* '"I'm a string"' -> false
*/
function tryParseJSONObject (jsonString){
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
}
}
catch (e) { }
return false;
};
No.
JSON is data-only. If you include a comment, then it must be data too.
You could have a designated data element called "_comment" (or something) that should be ignored by apps that use the JSON data.
You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.
But if you decided to:
{
"_comment": "comment text goes here...",
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
No, comments of the form //… or /*…*/ are not allowed in JSON. This answer is based on:
- https://www.json.org
- RFC 4627:
The
application/jsonMedia Type for JavaScript Object Notation (JSON) - RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format (supercedes RFCs 4627, 7158, 7159)
It's a data interchange format.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
http://json.org
JavaScript Object Notation is a text-based open standard designed for human-readable data interchange.
Type of format: Data interchange
http://en.wikipedia.org/wiki/Json
is it only defined in context of a language
Certainly not. It is entirely possible to write applications which use JSON but not JavaScript.
JSON is a data format. It could be classified as a language, but not a programming language.
Its relationship to JavaScript is that it shares its syntax (more or less) with a subset of JavaScript literals. The JSON specification defines it completely; it doesn't depend on references to the JavaScript specification.