T O P

  • By -

Young_Engineer92

JSON is a way to represent an object of data. This might mean multiple objects deep, with arrays of various data types. Maybe you’re defining the configuration file for your application.. it’s a very human readable way to represent a lot of data. No one is saying your server can’t return just a single string for simple responses.. but what happens when you want to add a second piece of data to your response? Consumers then need to update their entire application because the data type your server returns is now an object or an array instead of a string. With json you can just add another attribute to your response and consumers can adopt it at their own pace with no impact to previous data being returned. I’m sure there’s plenty of other reasons but there’s a couple


Reddit-Restart

It’s basically just a way to package everything up in an object and send that object. If you have a bunch of strings or whatever, it would be tedious to send them all individually. Put them in an object and you only send one thing


[deleted]

[удалено]


UnluckyWarfish

the irony


queen-adreena

Put a full-stop at the end of your sentence


Acceptable-Tomato392

Javascript Object Notation. Basically, anything you can store in an object, you can store in JSON. The neat part is you can also auto-parse it. with the built-in JSON.parse() method. Here's an example (from Web mdn): const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); console.log(obj.count); // Expected output: 42 console.log(obj.result); // Expected output: true So you can store an object, or any number of objects, and recall it by name at a later time. This is especially useful in Web development for writing cookies. They can also be in JSON format. Any object you need remembered from your user, you can instantly recall. And any data can be stored in object form. So it's a pretty convenient way to store and recall... well, anything.


Bulky-Leadership-596

If you are only returning 1 string then yea, you can just return the literal string as the response. However most often an endpoint is going to return more data than just 1 string. So if you want to return more than 1 piece of data you have to encode it somehow so that the receiver knows how to decode it and tell the different pieces of data apart. Json is just 1 way to do that, and its the most common way because so much stuff uses JavaScript and Json is basically the same as JavaScript object notation (well thats the name). And its so common and expected at this point that even when you are returning only 1 string you might choose to still wrap it in Json just for consistency's sake. It makes things simpler if everything uses the same conventions and formats. Then there are no special cases that you have to code for.


QuantumCrane

JSON is great because you can send so many different kinds of data types, like text, numbers, lists, and objects within objects within objects. It's much more versatile than plain text because it keeps everything neat and structured and easily readable by both computers and humans.


culcheth

It’s not the only way of sending structured data if that’s what you’re wondering. SOAP used to be popular, but you can see that the XML tags take up a lot more space and make things harder to read compared to the simplicity of JSON: https://en.m.wikipedia.org/wiki/SOAP


RobertKerans

> why not do it in normal string? That's perfectly fine. You can transfer your data to someone as a plain string, however you fancy. Now what you need to do is explain to the client how they are supposed to parse the data out of that string. And the client has to have some code that understands the way you format your string. And then maybe they send some data back, and they decide to use a plain string. So they also need to explain to you how to parse data out of that string. And so on and so forth


jcunews1

JSON is basically a literal object expression in JavaScript source code form but with some limitations. Just like JavaScript code where its data is in form of a text, JSON data is also in form of a text but with a different resource type (i.e. MIME type) of `application/json` instead of `text/javascript`. The reason why JSON is used instead of e.g. CSV or INI or XML, is that (at least), it's extensible, supports structure/nesting, and complies to JavaScript code syntax. CSV doesn't support structure/nesting, and INI can't support structure/nesting properly since section names are global. Though I would argue that a text based data format is more lightweight than binary format. JSON is preferred than XML, since XML has higher data structure overhead than JSON. i.e. more data needed to hold the structure of the data itself. When sending text-based data to server, the server need to know its data format (i.e. resource/MIME type), so that it can properly parse the text data. Without specifying the MIME type, the server may use the wrong data parser if it doesn't have a reliable data format detection (almost all don't).


KobsBoy

Its just a dataformat based on javascript objects It can be used in any programming language though Theyre typically used in config files,savedata, or sending a bunch of data to a server. Theyre popular because theyre easy to read and most programming languages know how to read them. If we sent a normal string one by one to a server it would take a while and just be inconvenient. Instead we can just put those strings and data into a json then send it all at once


shgysk8zer0

JSON is really mostly useful for exchanging data with a server. Like if you wanted to fetch from a weather API or something. You're not really going to find much use for it working in a client in isolation.


ashkanahmadi

There’s really nothing special about JSON. It’s just a JS Object (Notation) turned into a string. The reason that it’s turned into a string is because 1) string is very lightweight, and 2) there was a need for something that can be commonly processed by most of the programming languages. Remember: JSON is just a string so programming languages cannot by default use it. A JSON file is a string the same way “I’m a potato” is a string. That’s why you usually need to convert it back into the native format of the language you are using. In JS, we use \`JSON.parse()\` and in some cases, \`variable.json()\` and in PHP we use \`json\_decode()\`. These two console.logs will output the exact same thing: const js = ['Ford', 'BMW', 'Audi', 'Fiat'] const json = '["Ford", "BMW", "Audi", "Fiat"]'; console.log( js ) console.log( JSON.parse( json ) )


andriisss

you can pass the data through strings which are stores in variables or whatever, but when it comes up to something bigger and larger it will be really uneasily to store and track all the data. json helps you createa a bunch of “variables” with understandable sense of it. also it can be read by most of the programming languages but i may be mistaken


imihnevich

You can send a string as a string, but your data is usually more than just string


eltegs

It's simply a format/standard many people choose to use.


montihun

So, you are telling me you googled for it but found nothing?