JSON in Golang

Learn via video courses
Topics Covered

Overview

JSON (JavaScript Object Notation) is a language-independent data format used to store or represent structured data. JSON is used for communication between a web application and its server. In This article, we will learn about multiple operations on JSON Golang with an encoding library.

Introduction

Go offers built-in support for JSON encoding and decoding covering a wide range of encoding schemes. These packages are extremely easy to use and get started. Most don't require you to code anything for decoding and encoding. You plug in your data and it gives you the encoded/decoded values.

Go provides the encoding/json package to handle JSON Golang content via the encoding namespace of the standard library. The encoding/json package offers API functions for generating JSON documents from Go objects - and populating Go objects from the JSON documents. Package json implements encoding and decoding of JSON as defined in RFC 7159.

JSON encoding

Unmarshaling Raw JSON Data

Golang encoding package provides Unmarshal function as a standard library. It is used to convert []byte variable in a specified type object in Golang.

We can convert JSON strings into bytes and unmarshal the data.

Structured Data

Unmarshalling structured data is converting structured struct data from a JSON Golang using an encoding library. The unmarshal function takes a parameter for the struct type.

Output:

Structered data example :link

JSON Arrays

output:

JSON Arrays example : link

Nested Objects

output:

Nested Objects example : link

Primitive Types

We can unmarshal Go primitive data types such as integers, strings, floats, and booleans using the same Unmarshall method.

output:

Primitive Types example : link

JSON Struct Tags

Json struct Tags in Golang are meta-data we provide in the struct declaration. it is defined using "`" keyword.

output :

JSON Struct Tags example : link

Decoding JSON to Maps

output:

Decoding JSON to Maps example :link

Marshaling JSON Data

Marshalling data from json can be done using the Marshall method from encoding/json standard package.

Marshaling Structured Data

output:

Marshaling JSON Data example :link

Ignoring Empty Fields

We can ignore a field in struct by using the omit empty json tag in Golang.

output:

Ignoring Empty Fields example : link

Marshaling Slices

output :

Marshaling Slices example : link

Marshaling Maps

output:

Marshaling Maps example : link

Best Practices

  • Always Prefer to create a data struct for representing a JSON object in your application.
  • Only use a Map if you are unable to use a struct due to the uncertain value of input data.
  • For Maps we will require every key to be of the same data type or use a generic type.

Libraries for Accelerating JSON marshaling/unmarshalling

Comparing Libraries

  • Encoding/json is a great solution for working with small objects with a small load. It gives consistent performance forbag rise in load.
  • easyjson and ffjson work 3 times faster than fastjson for both encoding and decoding.
  • fast JSON is fast for small json size but performance degrades as the size of json increases.

Conclusion

  • We learned about Json in Golang
  • We implemented various Marshalling and Unmarshalling examples in JSON golang using an encoding library.
  • We discussed different libraries in Golang for JSON manipulation.