GoLang : Dynamic JSON Parsing using empty Interface and without Struct in Go Language
JSON(JavaScript Object Notation) parsing a day to activity for a developer. Most of the API which developer parse are in JSON.
Here we will see how we can parse JSON Object and Array using GoLang Interfaces. This reduce overhead to creating struct when data is unstructured and we can simply parse the data and get the desire value from the JSON.
This article will explain parsing different type of JSON.
- Simple object JSON
- Simple array JSON
- Embedded object in JSON
- Embedded object in array of JSON
My strategy would be learn by doing!!! writing the code first and providing the explanation always works for me.
1. Parsing Simple object JSON:
Output:
Id : 11
Name : Irshad
Department : IT
Designation : Product Manager
Try it at : https://play.golang.org/p/JlSoK1t1CkO
Code Explanation:/* First: declared map of string with empty interface
which will hold the value of the parsed json. */
var result map[string]interface{}/* Second: Unmarshal the json string string by converting
it to byte into map */
json.Unmarshal([]byte(empJson), &result)/* Third: Read the Value by its key */
fmt.Println("Id :", result["id"])
2. Parsing Simple Array JSON:
Output:
Reading Value for Key : 0
Id : 1 - Name : Mr. Boss - Department : - Designation : Director
Reading Value for Key : 1
Id : 11 - Name : Irshad - Department : IT - Designation : Product Manager
Reading Value for Key : 2
Id : 12 - Name : Pankaj - Department : IT - Designation : Team Lead
Try it at : https://play.golang.org/p/A9Nr4TZqtgB
Code Explanation:/* First: declared array map of string with empty interface
which will hold the value of the parsed json. */
var results []map[string]interface{}/* Second: Unmarshal the json string string by converting
it to byte into map */
json.Unmarshal([]byte(empArray), &results)/* Third: Read the each item from an array of map using range */
for key, result := range results {}/* Fourth: Read the Value by its key inside range loop */
fmt.Println("Id :", result["id"])
3. Parsing Embedded object in JSON
Output:
Id : 11
Name : Irshad
Department : IT
Designation : Product Manager
Address : Mumbai Maharashtra India
Try it at : https://play.golang.org/p/X4Z9CPwG5JL
Code Explanation:/* First: declared map of string with empty interface
which will hold the value of the parsed json. */
var result map[string]interface{}/* Second: Unmarshal the json string string by converting
it to byte into map */
json.Unmarshal([]byte(empJson), &result)/* Third: To Read data from address node we have to again declare one more map with empty interface */
address := result["address"].(map[string]interface{})/* Fourth: Read the Value from main node */
fmt.Println("Id :", result["id"])/* Fifth: Read the Value from address node */
fmt.Println("City :", address["city"])
4. Parsing Embedded object in Array of JSON
Output:
Reading Value for Key : 0
Id : 1 - Name : Mr. Boss - Department : - Designation : Director
Address : Mumbai Maharashtra India
Reading Value for Key : 1
Id : 11 - Name : Irshad - Department : IT - Designation : Product Manager
Address : Mumbai Maharashtra India
Reading Value for Key : 2
Id : 12 - Name : Pankaj - Department : IT - Designation : Team Lead
Address : Pune Maharashtra India
Try it at : https://play.golang.org/p/P54UcFy4uJt
Code Explanation:/* First: declared array map of string with empty interface
which will hold the value of the parsed json. */
var results []map[string]interface{}/* Second: Unmarshal the json string string by converting
it to byte into map */
json.Unmarshal([]byte(empArray), &results)/* Third: Read the each item from an array of map using range */
for key, result := range results {}/* Fourth: To Read data from address node from each item we have to declare one more map with empty interface inside range loop */
address := result["address"].(map[string]interface{})/* Fifth: Read the result value by its key inside range loop */
fmt.Println("Id :", result["id"])/* Sixth: Read the address value by its key inside range loop */
fmt.Println("City :", address["city"])
Parsing JSON in go is very each and its depends upon developer preferences whether they want to use struct or they want to parse them raw using interfaces.
Will keep posting new articles! Thank you!!!