Home > Backend Development > Golang > How to Parse Nested JSON Data with Unknown Keys and Unconventional Field Structures?

How to Parse Nested JSON Data with Unknown Keys and Unconventional Field Structures?

Linda Hamilton
Release: 2024-11-16 21:22:03
Original
616 people have browsed it

How to Parse Nested JSON Data with Unknown Keys and Unconventional Field Structures?

Unraveling the Enigma of Nested JSON with Unknown Keys

Unmarshaling complex JSON data with unknown keys can be a daunting task. Consider the perplexing JSON structure provided:

{
  "message": {
    "Server1.example.com": [
      {
        "application": "Apache",
        "host": {
          "name": "/^Server-[13456]/"
        },
        "owner": "User1",
        "project": "Web",
        "subowner": "User2"
      }
    ],
    "Server2.example.com": [
      {
        "application": "Mysql",
        "host": {
          "name": "/^Server[23456]/"
        },
        "owner": "User2",
        "project": "DB",
        "subowner": "User3"
      }
    ]
  },
  "response_ms": 659,
  "success": true
}
Copy after login

The structure is confusing, but we can dissect it step by step. Initially, we note the presence of unknown server names, such as "Server1.example.com" and "Server2.example.com," which vary dynamically. Additionally, the "host" field contains a cryptic regex expression without an enclosing key.

To unravel this challenge, let's shift our focus to the provided struct:

type ServerDetails struct {
  Message  struct{
    Hostname struct{
      Details struct{
        Application string `json:"application"`
        }`json:"-"`
       }`json:"-"`
     }`json:"message"`
}
Copy after login

This struct is designed to handle the known fields within the "message" object, but it fails to capture the varying server names and the peculiar "host" field.

A key insight is to utilize a more flexible data structure, namely a map[string]ServerStruct. This allows us to account for the unknown server names as keys that point to a ServerStruct value.

The revised struct would resemble:

type YourStruct struct {
    Success bool
    ResponseMS int
    Servers map[string]*ServerStruct
}

type ServerStruct struct {
    Application string
    Owner string
    [...]
}
Copy after login

With the addition of appropriate JSON tags, we can now successfully parse the JSON data into this enhanced struct.

By adapting to the dynamic nature of the JSON data using a map[string]ServerStruct and embracing the possibilities of unkeyed fields with JSON tags, we can effectively unravel the perplexing structure of nested JSON data and extract the desired information.

The above is the detailed content of How to Parse Nested JSON Data with Unknown Keys and Unconventional Field Structures?. 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