Home > Backend Development > Golang > How to Efficiently Parse Nested JSON in GoLang Using Type Assertion?

How to Efficiently Parse Nested JSON in GoLang Using Type Assertion?

Patricia Arquette
Release: 2024-11-26 08:06:12
Original
502 people have browsed it

How to Efficiently Parse Nested JSON in GoLang Using Type Assertion?

Looping Over Nested JSON in GoLang

A requirement exists to extract key-value pairs from a nested JSON structure for dynamic processing. To efficiently traverse this structure, the following approach is recommended:

Using Type Assertion with Range Loop

As provided in the response, a type assertion can be used to iteratively access and process the contents of nested JSON objects. The following sample code demonstrates this approach:

package main

import (
    "encoding/json"
    "fmt"    
)

func main() {
    // Create a map for the JSON data
    m := map[string]interface{}{}
    
    // Unmarshal the JSON input into the map
    err := json.Unmarshal([]byte(input), &m)
    if err != nil {
        panic(err)
    }

    // Recursively parse the map
    parseMap(m)
}

func parseMap(aMap map[string]interface{}) {
    for key, val := range aMap {
        switch concreteVal := val.(type) {
        case map[string]interface{}:
            fmt.Println(key)
            parseMap(concreteVal)
        case []interface{}:
            fmt.Println(key)
            parseArray(concreteVal)
        default:
            fmt.Println(key, ":", concreteVal)
        }
    }
}

func parseArray(anArray []interface{}) {
    for i, val := range anArray {
        switch concreteVal := val.(type) {
        case map[string]interface{}:
            fmt.Println("Index:", i)
            parseMap(concreteVal)
        case []interface{}:
            fmt.Println("Index:", i)
            parseArray(concreteVal)
        default:
            fmt.Println("Index", i, ":", concreteVal)
        }
    }
}

const input = `
{
    "outterJSON": {
        "innerJSON1": {
            "value1": 10,
            "value2": 22,
            "InnerInnerArray": [ "test1" , "test2"],
            "InnerInnerJSONArray": [{"fld1" : "val1"} , {"fld2" : "val2"}]
        },
        "InnerJSON2":"NoneValue"
    }
}
`
Copy after login

In this code, the parseMap and parseArray functions recursively traverse the nested JSON structure, printing the key-value pairs in a hierarchical manner. This approach provides a versatile mechanism to access and process data from JSON regardless of its complexity.

The above is the detailed content of How to Efficiently Parse Nested JSON in GoLang Using Type Assertion?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template