Home > Backend Development > Golang > How can I unmarshal nested JSON data without knowing its structure?

How can I unmarshal nested JSON data without knowing its structure?

Patricia Arquette
Release: 2024-11-02 16:55:03
Original
457 people have browsed it

How can I unmarshal nested JSON data without knowing its structure?

Unmarshalling Nested JSON Without Known Structure

When encountering nested JSON data without defined structures, there are several approaches to mitigate challenges in unmarshalling.

Avoiding Repeated Unmarshals

Minimizing unmarshalling operations is generally advisable. Consider implementing a caching mechanism to store unmarshalled objects for future use and avoid repetitive unmarshalling. However, in some cases, multiple unmarshalling may be necessary, especially when dealing with nested structures of varying types.

Determining the Correct Struct for Unmarshalling

Method 1: Unmarshal to map[string]interface{}

Unmarshal the json.RawMessage into a map[string]interface{}. This allows examination of the nested structure to identify the type and, subsequently, the correct struct to unmarshal into.

Example:

<code class="go">var objMap map[string]interface{}
json.Unmarshal(rawMessage, &objMap)</code>
Copy after login

Method 2: Regular Expression Match

Use a regular expression to match the type string within the JSON data. Once the type is known, use reflection or a type switch to unmarshal into the corresponding struct.

Example:

<code class="go">type Regex *regexp.Regexp

// Split the JSON data into key-value pairs
type KeyValue struct {
    Key   string
    Value string
}

// Regex for extracting the type
var typeRE = Regex(regexp.MustCompile(`(?m)^.*"type": "(.+)".*$`))

// Unmarshal the raw message and extract the type
func getType(rawMessage []byte) (string, error) {
    var data KeyValue
    err := json.Unmarshal(rawMessage, &data)
    if err != nil {
        return "", err
    }
    matches := typeRE.FindStringSubmatch(data.Value)
    return matches[1], nil
}</code>
Copy after login

Using the Copy or Regular Expression Approach

Method A: Copy and Unmarshal

  1. Create a copy of the json.RawMessage.
  2. Unmarshal the copy into an interface{} to identify the type.
  3. Unmarshal the original json.RawMessage into the struct of the determined type.

Method B: Regular Expression and Unmarshal

  1. Use the regular expression to determine the type.
  2. Use reflection or a type switch to create a new instance of the struct with the determined type.
  3. Unmarshal the json.RawMessage into the newly created struct.

The above is the detailed content of How can I unmarshal nested JSON data without knowing its structure?. 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