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.
What is JSON and what is it used for? - Stack Overflow
ELI5: What is the point of JSON and how does it work?
JSON is a data interchange format (or serialization format.) If you have some object and you want to store it in a file or send it over a network connection or something of that nature, you have to convert it to a string first, because you can't send objects over a network or write objects to a file. You can only write strings. And the other end has to receive that string and reconstitute it into an object before it can work with it. JSON is a set of rules for how to turn objects into strings and vice versa. It's based on JavaScript syntax, but it's not specific to JavaScript; it's used by many other languages. Its cousins are other serialization formats such as YAML and XML.
toString() is completely useless for this. When invoked on an object, it evaluates to the string "[object Object]". Example:
> var obj = {foo: 42, bar: [1, 2, 3], baz: "hippo"};
undefined
> obj.toString();
'[object Object]'
> JSON.stringify(obj);
'{"foo":42,"bar":[1,2,3],"baz":"hippo"}'Note that in this example you could have just put quotes around the object literal, but that's missing the point. You don't always have an object literal. The object could have been built by much more complicated means. This is just a simple example.
More on reddit.comWhat is JSON?
Javascript object notation.
Its a way of sending and receiving data in a certain format. In the old days if I wanted to exchange data with (for example) Hertz I might call them up and agree to send them a fixed width or comma separated values (CSV) file with request information and they might agree to send a similar fromatted response file or a completely different format. If my colleague contacted them they might also agree on an interface but his request file might be a different format to mine and their response might also be a different format. Hertz may want a fixed width request file but Budget may want me to send a CSV request file. So loads of different interfaces existed (Hertz may be different to Budget, who are also different from AVIS etc.). And Im just using rental car companies as an example - loads of different comanies have interfaces and exchange information all the time.
My request file to Hertz (formatted as we agreed):
REQUEST=WhoIsDriver, DATE=01/01/2010, Plate="XYZ 1234"
Their response (formatted however they choose):
DRIVER:JoeSmith, CAR:Ford, MODEL: Explorer, FROM: 12/30/2009, TO: 01/07/2010
So in the old days it was truly random with all sorts of interface formats whcih were proprietary to whoever designed them Then came along XML, which was. better in that it forced a certain kind of syntax but it was very wordy
<request>
<type>WhoIsDriver</type>
<date>01/01/2011</date>
<plate>XYZ 1234</plate>
</request>
This was very popular and the forced formatting meant lots of standardized services appeared on the WEB saying if you send us data formatted in XML in a certain way we will send back information to you also formatted in XML. Google SOAP interfaces for more info on this.
But in a trifector of luck/timing/ whatever:
XML is very wordy, If you have an interface with 10,000 requests and responses the file sizes get huge because of all the <start-tag>...<end-tag> stuff, and this makes things slow.
everyone is slowly moving to new shiny web front-ends for their systems and therefore having to use Javascript, and
Javascript have a nifty less wordy way of representing data - their Javascript object notation (also called JSON).
So slowly but surely, JSON is taking over the world in interfaces between companies and also many public interfaces (such a Google maps) that used to be XML only are now becoming REST services/interfaces that use JSON format for data exchange.
Javascript (JSON) format is:
{ "type" : "WhoIsDriver", "date" : "01/01/2011", "plate" : "XYZ 1234" }
In the web development world we frequently have to pass data from the front-end programs in the browser to the back-end programs on the server. This is also an interface and nowadays most data is passed on JSON format.
More on reddit.comELI5 what a JSON file is
JSON is a convention or protocol for formatting and structuring data you want to store. The basic idea is that information and data is very important just in general for just about anything you might possibly might want to do, which means being able to easily access and manipulate the data you own is also very important.
When you have information (demographic data, some representation of a CAD model, numbers computed from research, whatever), you could just take that data, chuck it into a text file, and write some custom code to try and extract and write to that file in some manner. This however is somewhat of a fragile solution since anybody else who wants to work with your data would also need to write some custom code to parse however your data is formatted.
This is obviously sub-optimal -- being able to freely exchange and work with data is nearly as important as the data itself.
So, what people did was invent standards for how data should be formatted and structured (and took care to make sure those standards were flexible enough to represent all kinds of data). The idea is then that if somebody creates a library that can understand and work with that format, everybody else can use that library without having to constantly reinvent things. As they say, don't reinvent the wheel.
XML is one such standard. CSV is another. JSON is yet another. There are many.
JSON, in particular, was designed to be simple and minimalistic. The rules for what constitutes a valid chunk of JSON were designed to very basic and straightforward, and to closely mimic data structures and primitive types that many programming languages have built-in by default.
For example, here is a basic example of some invented data I came up with structured in JSON format:
{
"data-type": "posts",
"posts": [
{
"title": "Comment 1",
"body": "..."
},
{
"title": "Comment 2",
"body": "..."
}
]
}If you have any experience working with languages like JavaScript or Python, you'll notice this looks identical to dict, list, and string literals. This resemblance is identical.
For comparison is how you might decide to structure this same data in a different format, XML:
<data type="posts">
<comment>
<title>Comment 1</title>
<body>...</body>
</comment>
<comment>
<title>Comment 2</title>
<body>...</body>
</comment>
</data>Learning JSON (or XML or any data exchange format) is not typically the hard part and is typically easily learned and mastered. Working with the underlying data as well as tools that can manipulate your data, store it into various forms may potentially be hard.
More on reddit.comWhat is a JSON file?
How is a JSON file structured?
Who works with JSON files?
Videos
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language (the way objects are built in JavaScript). As stated in the MDN, some JavaScript is not JSON, and some JSON is not JavaScript.
An example of where this is used is web services responses. In the 'old' days, web services used XML as their primary data format for transmitting back data, but since JSON appeared (The JSON format is specified in RFC 4627 by Douglas Crockford), it has been the preferred format because it is much more lightweight
You can find a lot more info on the official JSON web site.
JSON is built on two structures:
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
JSON Structure





Here is an example of JSON data:
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}
JSON in JavaScript
JSON (in JavaScript) is a string!
People often assume all JavaScript objects are JSON and that JSON is a JavaScript object. This is incorrect.
In JavaScript, var x = {x:y} is not JSON, this is a JavaScript object. The two are not the same thing. The JSON equivalent (represented in the JavaScript language) would be var x = '{"x":"y"}'. x is an object of type string not an object in its own right. To turn this into a fully fledged JavaScript object you must first parse it, var x = JSON.parse('{"x":"y"}');, x is now an object but this is not JSON anymore.
See JavaScript object vs. JSON
When working with JSON and JavaScript, you may be tempted to use the eval function to evaluate the result returned in the callback, but this is not suggested since there are two characters (U+2028 & U+2029) valid in JSON but not in JavaScript (read more of this here).
Therefore, one must always try to use Crockford's script that checks for a valid JSON before evaluating it. Link to the script explanation is found here and here is a direct link to the JavaScript file. Every major browser nowadays has its own implementation for this.
Example on how to use the JSON parser (with the JSON from the above code snippet):
// The callback function that will be executed once data is received from the server
var callback = function (result) {
var johnny = JSON.parse(result);
// Now, the variable 'johnny' is an object that contains all of the properties
//from the above code snippet (the JSON example)
alert(johnny.firstName + ' ' + johnny.lastName); // Will alert 'John Smith'
};
The JSON parser also offers another very useful method, stringify. This method accepts a JavaScript object as a parameter, and outputs back a string with JSON format. This is useful for when you want to send data back to the server:
var anObject = {name: "Andreas", surname : "Grech", age : 20};
var jsonFormat = JSON.stringify(anObject);
// The above method will output this: {"name":"Andreas","surname":"Grech","age":20}
The above two methods (parse and stringify) also take a second parameter, which is a function that will be called for every key and value at every level of the final result, and each value will be replaced by result of your inputted function. (More on this here)
Btw, for all of you out there who think JSON is just for JavaScript, check out this post that explains and confirms otherwise.
References
- JSON.org
- Wikipedia
- Json in 3 minutes (Thanks mson)
- Using JSON with Yahoo! Web Services (Thanks gljivar)
- JSON to CSV Converter
- Alternative JSON to CSV Converter
- JSON Lint (JSON validator)
The Concept Explained - No Code or Technical Jargon
What is JSON? – How I explained it to my wifeTM
Me: “It’s basically a way of communicating with someone in writing....but with very specific rules.
Wife: yeah....?
Me: In prosaic English, the rules are pretty loose: just like with cage fighting. Not so with JSON. There are many ways of describing something:
• Example 1: Our family has 4 people: You, me and 2 kids.
• Example 2: Our family: you, me, kid1 and kid2.
• Example 3: Family: [ you, me, kid1, kid2]
• Example 4: we got 4 people in our family: mum, dad, kid1 and kid2.
Wife: Why don’t they just use plain English instead?
Me: They would, but remember we’re dealing with computers. A computer is stupid and is not going to be able to understand sentences. So we gotta be really specific when computers are involved otherwise they get confused. Furthermore, JSON is a fairly efficient way of communicating, so most of the irrelevant stuff is cut out, which is pretty hand. If you wanted to communicate our family, to a computer, one way you could do so is like this:
{
"Family": ["Me", "Wife", "Kid1", "Kid2"]
}
……and that is basically JSON. But remember, you MUST obey the JSON grammar rules. If you break those rules, then a computer simply will not understand (i.e. parse) what you are writing.
Wife: So how do I write in Json?
A good way would be to use a json serialiser - which is a library which does the heavy lifting for you.
Summary
JSON is basically a way of communicating data to someone, with very, very specific rules. Using Key Value Pairs and Arrays. This is the concept explained, at this point it is worth reading the specific rules above.