Introduction
JavaScript Object Notation (JSON) is a widely used data format built from JavaScript's object literals as defined by the ECMAScript computer language standard. It is an introductory textual format that uses UTF-8 encoding and is primarily used for data storage and transmission. The self-descriptive nature of data representation improves readability and is frequently used as a data exchange intermediate in web programming and client-server communication.
The article will demonstrate how to work with JSON files using Go's standard library.
For decoding and encoding, the data types are:
- bool representing JSON boolean
- float64 representing JSON numbers
- string representing JSON strings
- nil representing JSON null
- array representing JSON array
- map representing JSON object
- struct representing JSON object
Storing Data in JSON
Strings, integers, Booleans, and null are the four primitive kinds that JSON can represent. Structured types, such as objects and arrays, can also be represented using it. In JSON, a string is a series of 0 or more Unicode characters, whereas an object is an unordered collection of 0 or more named/value pairs. The name of the name/value pair is a string, while the value can be any primitive or structured type, including string, integer, Boolean, null, object, or array. A series of zero or more values are represented by an array.
Data in JSON is represented using key-value pairs separated by commas, and curly braces are used to enclose the objects.
Example:
{
"roll_no": 12,
"name" : "Apurv",
"subjects": {
"core": ["calculus","DSA","DBMS"],
"non-core": ["economics","marketing","existentialism"]
}
}
The above shows an example of a JSON object. It contains key/value pairs such as (“roll_no”,12) and ("name" : "Apurv"). It also contains another object named “subjects,” which also has its key/value pairs. In this fashion, an object might have several nested inner objects that are all represented in the same way.
The inner object contains a key “core,” which has value represented by an array of values enclosed by square brackets.