What's the use of JSON?

Basically JSON is a common data transport layer, so that different systems can easily communicate structured data with each other.

2 Likes

You could, but then Ajax requests are blocked, too. And I’d think fetch requests as well?

Without saving it as a file on the server first? So, directly?

Yes JSON can be (and commonly is) used directly, no saving on the server is needed.

1 Like

A lot of APIs require some form of authentication to allow a request to proceed.
If you want to restrict access to your PHP endpoint, you can set this up.

To recap what has been said, JSON can be used to store data (and is), but it is mostly used for transfer of data. A client’s request to a server, a server’s response to a client request.
So your PHP server may well still use SQL to store data, but when the client makes a request, the server pulls the data from the DB, formats it to JSON and sends to the client in response, JS can do the rest on that side.

Another use case where I have used JSON is in structured data mark-up. That can be set-up server side and included in the HTML page as it’s generally static. It’s just a neat way of presenting that data to Google in a way it understands, without too much complex messing up the of HTML mark-up as structured data was previously done.

4 Likes

As an aside…

Deep Cloning

JSON supports:

  1. primitive numbers and strings
  2. values true, false and null
  3. objects and arrays built up of the above types, including nested objects and arrays.

As long as your object only contains the above types, you can use a combination of JSON.stringify and JSON.parse to make a deep clone of that object. e.g.

const user = {
    id: 1,
    name: ['Bob', 'Smith'],
    email: 'bob@gmail.com',
    address: {
      street: '1745 T Street Southeast',
      city: 'Washington',
      postalCode: '20020'
    }
}

// serialize and deserialize user object
const deepClonedUser = (JSON.parse(JSON.stringify(user)))

// changes to cloned object only
deepClonedUser.name[0] = 'Robert'
deepClonedUser.address.postalCode = '20050'

Json.stringify can also be quite useful for logging out the properties of an object and maintain the original order.

console.log(JSON.stringify(user, null, 2 /* indentation */))
console.log(JSON.stringify(deepClonedUser, null, 2))

// user
{
  "id": 1,
  "name": [
    "Bob",
    "Smith"
  ],
  "email": "bob@gmail.com",
  "address": {
    "street": "1745 T Street Southeast",
    "city": "Washington",
    "postalCode": "20020"
  }
}

// deepClonedUser
{
  "id": 1,
  "name": [
    "Robert", // <-- only changed on clone
    "Smith"
  ],
  "email": "bob@gmail.com",
  "address": {
    "street": "1745 T Street Southeast",
    "city": "Washington",
    "postalCode": "20050" // <-- only changed on clone
  }
}

15 posts were split to a new topic: NoSQL vs traditional databases

JSON is used extensively for storing data. For example see Data Files | Jekyll • Simple, blog-aware, static sites. It says:

Jekyll supports loading data from YAML, JSON, CSV, and TSV files located in the _data directory.

See Configuration in ASP.NET Core | Microsoft Docs; ASP.NET Core uses JSON for configuration data; appsettings.json and launchSettings.json.

There are thousands of other such examples.

See JSON; it says JSON (JavaScript Object Notation) is based on a subset of the JavaScript language. There has been very much that has evolved from JSON; many philosophies, like religions. The original use of JSON was in JavaScript for creating data in memory. It is not really objects even, since JSON has no members (in OOP they are called members; JavaScript calls them functions).

JSON is just a way to represent data. The simplicity makes it very useful.

I wish everything was that easy to explain. It is not that easy to get explanations of many other computer things, like Docker and Kubernetes. Determining the relevance of complicated software such as those can take months before being able to know if they are useful to you.

1 Like

Data in other formats, including CSV, XML, database and spreadsheet, can be converted to/from JSON. It is possible to write an interactive (GUI) application to create (and maintain) data in any of those formats.

1 Like

Wow, interesting that you give us the best example why “Jumping on the new train” is never a good idea. 2 Years ago everyone told you that “Docker” is the future. It is the best ever seen… etc

What? Docker and containerization is largely relevant in field. I just don’t like it. From a ui developer perspective I would rather spend time building experiences than fiddling with the infrastructure to host in a production quality environment. Anyone can write a lambda and just push it to the web. Anyone can’t just setup Kubernetes. Kubernetes solves a lot of scaling problems that traditional servers and even vms don’t easily solve. However, that comes at a price of complexity and cost.

OK - the topic under discussion here is JSON. Please keep to the subject at hand out of courtesy to the OP.

If you wish to go off on a tangent, then by all means open a new thread to do so. Use the “New topic” button accessible from the post number or datestamp to start a new thread with a link back to the relevant post.

new-topic

Alternatively, use the J and K keys to navigate to the post, and then T to open the new topic.

7 Likes

That is correct. I only mentioned other technologies as examples, I did not intend to discuss them any further than that.

Thanks all for your replies. I’ve got some studying to do, but I now see that JSON can be very useful.

3 Likes

I meant methods, not members. Something in my head kept trying to tell me that and finally I listened.

1 Like

im trying to get into what is json in real project… so you mean that json have nothing do with server, we simple store data in json file, and access needed data from json file rather from server…???

so can json communicate with database and retrieve data and update data in database…
can json read and update data in database…

Suppose we have the following CSV data.

Id,Model,ASIN,Price,Comments,HDR10
1,Samsung BD-H5900,B00KGERL3Y,219,WiFi,No
2,Sony BDPS6700,B07H47458S,144,Network,No
3,NeeGo Sony UBP-X700,B07BFHZC31,200,Network,Yes
4,Samsung UBD-M8500,B07KKW1QRL,225,No network,No
5,Sony BDP-S6500,B01E4NDS48,209,Network,No
6,LG UP870,B0795CJ3XH,259,No network,Yes

Note that that data does not use quotes to delimit text with spaces; CSV files vary in format like that.

Go to an online converter site such as Best online CSV to JSON and CSV to XML Converter/Transformer tool. There are many of them and I do not consider any to be the best. When I convert the preceding data to XML I get:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
	<0>
		<Id>1</Id>
		<Model>Samsung BD-H5900</Model>
		<ASIN>B00KGERL3Y</ASIN>
		<Price>219</Price>
		<Comments>WiFi</Comments>
		<HDR10>No</HDR10>
	</0>
	<1>
		<Id>2</Id>
		<Model>Sony BDPS6700</Model>
		<ASIN>B07H47458S</ASIN>
		<Price>144</Price>
		<Comments>Network</Comments>
		<HDR10>No</HDR10>
	</1>
	<2>
		<Id>3</Id>
		<Model>NeeGo Sony UBP-X700</Model>
		<ASIN>B07BFHZC31</ASIN>
		<Price>200</Price>
		<Comments>Network</Comments>
		<HDR10>Yes</HDR10>
	</2>
	<3>
		<Id>4</Id>
		<Model>Samsung UBD-M8500</Model>
		<ASIN>B07KKW1QRL</ASIN>
		<Price>225</Price>
		<Comments>No network</Comments>
		<HDR10>No</HDR10>
	</3>
	<4>
		<Id>5</Id>
		<Model>Sony BDP-S6500</Model>
		<ASIN>B01E4NDS48</ASIN>
		<Price>209</Price>
		<Comments>Network</Comments>
		<HDR10>No</HDR10>
	</4>
	<5>
		<Id>6</Id>
		<Model>LG UP870</Model>
		<ASIN>B0795CJ3XH</ASIN>
		<Price>259</Price>
		<Comments>No network</Comments>
		<HDR10>Yes</HDR10>
	</5>
</root>

When I convert the CSV data to JSON I get:

[
	{
		"Id": "1",
		"Model": "Samsung BD-H5900",
		"ASIN": "B00KGERL3Y",
		"Price": "219",
		"Comments": "WiFi",
		"HDR10": "No"
	},
	{
		"Id": "2",
		"Model": "Sony BDPS6700",
		"ASIN": "B07H47458S",
		"Price": "144",
		"Comments": "Network",
		"HDR10": "No"
	},
	{
		"Id": "3",
		"Model": "NeeGo Sony UBP-X700",
		"ASIN": "B07BFHZC31",
		"Price": "200",
		"Comments": "Network",
		"HDR10": "Yes"
	},
	{
		"Id": "4",
		"Model": "Samsung UBD-M8500",
		"ASIN": "B07KKW1QRL",
		"Price": "225",
		"Comments": "No network",
		"HDR10": "No"
	},
	{
		"Id": "5",
		"Model": "Sony BDP-S6500",
		"ASIN": "B01E4NDS48",
		"Price": "209",
		"Comments": "Network",
		"HDR10": "No"
	},
	{
		"Id": "6",
		"Model": "LG UP870",
		"ASIN": "B0795CJ3XH",
		"Price": "259",
		"Comments": "No network",
		"HDR10": "Yes"
	}
]

All three are each a way to store data. There are advantages and disadvantages to each. JSON can do more than CSV and XML can do more than JSON. JSON can be a balance of:

  • simplicity for the computer and for people
  • usually satisfying all the requirements
1 Like

Yes, all the above can be done with JSON. You can replace a traditional database architecture completely with JSON. You can store JSON and query the storage using JSON to retrieve the stored documents. The best comparison for someone with a relational database background is MongoDB. Check out MongoDB to understand how NoSQL works with JSON. That said there are a ton of alternate relational database technologies that run on JSON. Like Open Search for searching unstructured data in JSON.

The JSON file may well be located on a server, it’s just a file and data format like CSV or XML. Also, it doesn’t do anything by itself such as reading or storing data, rather data is being read from or stored as a JSON file.