#Golang-Json
9 articles
09 How To Use Streaming Encoder
Introduction to Stream Encoders
Apart from JSON Decoder, this JSON package can also support Encoder which is used directly JSON into io.Writer
. So that way we don’t need to store the JSON data first into a string
or []byte
variable, so we can just write it directly into io.Writer
.
08 How To Use Streaming Decoder
Introduction to Stream Decoder
In the previous article, we studied the JSON package by using conversion of JSON data which was already in the form of variables and string data or we used []byte
. Sometimes the JSON data comes from input in the form of io.Reader, which can be in the form of a file, network or request body. So we could just read all the data first, then store it in a variable and then convert it from JSON, but we don’t need to do this because the JSON package has a feature for reading data from Stream
.
07 How To Use Maps
Map Use
When we use JSON we sometimes encounter cases of dynamic JSON data, which means that the attributes formed by JSON are uncertain, can increase or decrease and are not even fixed. So in cases like this, if we unmarshal
using Struct, it will be difficult because in the struct we have to define all the attributes.
06 How to know JSON Tags
Introduction to JSON Tags
By default, if we create a struct and marshal it, it will be mapped according to the same attribute name and is case sensitive. So what if we want to differentiate the style between naming attributes in structs and those converted into JSON? For example, in structs we use PascalCase
but in JSON we want to use snake_case
. To support this, the json package supports Tags Reflection, namely tags that can adjust the Json name which we will later convert into JSON so that the tribute in the struct will be converted according to the name into JSON.
05 How to know JSON Array
Introduction to JSON Arrays
Apart from JSON in Object form, usually in JSON we can also use the Array data type. Actually, JSON arrays are similar to those in JavaScript where there are primitive data types or complex data types such as object arrays.
04 How to know Decode JSON
Introduction to JSON Decoding
In the previous article we learned to encode JSON and how to create a JSON object, so next we will learn how to translate JSON objects into struct objects in Golang. We often call this conversion from JSON into a Golang struct object as decode
.
03 How to know JSON Object
Introduction to JSON Objects
In the previous article we studied encoding data such as string
, number
, boolean
and other primitive types. But there is one case in the unit test which is made in the form of a struct object. So, this time we will study the objects of JSON in more depth. JSON data is in the form of objects and arrays, while the values can be objects again or directly data with other primitive types.
02 Encode JSON
Introduction to JSON Encoding
The Golang language already provides functions for JSON data conversion needs, namely we can use this function
01 Introduction Package
Introduction to JSON
JSON stands for JavaScript Object Notation which is a data format structure that looks like Object from JavaScript. JSON is the most widely used data format currently for creating RESTful APIs. All languages can use the JSON format because it can become a standard format for communication between services even if they use different languages. For example, one service uses Golang and another service uses Java, so that it can communicate well it needs the same format, namely JSON, which can standardize the format of the two languages in our service. You can visit https://json.org/json-en.html if you want to learn more.