Home > Backend Development > Golang > Using struct to parse nested json in go language

Using struct to parse nested json in go language

WBOY
Release: 2024-02-06 08:15:09
forward
429 people have browsed it

Using struct to parse nested json in go language

Question content

Unable to use go lang to parse nested json into a structure object

I have a nested json string and I want to parse it using structures in the go language. json looks like this

{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}
Copy after login

I want to parse json using go language. json has nested structure so I created the structure mentioned in the following code

package main

import (
    "encoding/json"
    "fmt"
)


type objecttagslist struct {
    tagcode  string
    tagname  string
    tagvalue []string
}

type model struct {
    action   string `json:"action"`
    business struct {
        listid     int64  `json:"listid"`
        objecttags []objecttagslist `json:"objecttags"`
    } `json:"business"`
}

func main() {
    json := `{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}`

    var model model
    json.unmarshal([]byte(json), &model)

    fmt.println(model.action) // this prints correctly as "add"
        fmt.println(model.business.listid) // this prints correctly as "123"


    fmt.println(model.business.objecttags) // this does not print the objecttags. rather this prints the objecttags as "[{  []} {  []}]"


}
Copy after login

I can't get the value of the inner nested json into the structure.

I also tried to unmarshal the internal structure again

var object []objecttagslist

//this gives error as cannot convert model.business.objecttags (variable of type []objecttagslist) to type []byte

json.unmarshal([]byte(model.business.objecttags), &object)
Copy after login

//Error, cannot convert model.business.objecttags (variable of type []objecttagslist) to type []byte

fmt.println(object)
Copy after login

This gives me an error Unable to convert model.business.objecttags (variable of type []objecttagslist) to type []byte.

How to map this json into a structure? I want to map it in such a way that I can use an object like

model.Business.ObjectTags[0].tagCode //--> Should print/store "csharp"
model.Business.ObjectTags[0].tagValue[0] //--> Should print/store "2"
Copy after login

Please help


Correct answer


You can only marshal/unmarshal "exported" fields - i.e. fields that can be accessed outside the current package, this is In go it means "fields starting with a capital letter". So if you were to modify your code to look like this:

package main

import (
    "encoding/json"
    "fmt"
)

type objecttagslist struct {
    tagcode  string
    tagname  string
    tagvalue []string
}

type model struct {
    action   string `json:"action"`
    business struct {
        listid     int64            `json:"listid"`
        objecttags []objecttagslist `json:"objecttags"`
    } `json:"business"`
}

func main() {
    json := `
{
  "action": "add",
  "business": {
    "listid": 123,
    "objecttags": [
      {
        "tagcode": "csharp",
        "tagname": "codename",
        "tagvalue": [
          "2"
        ],
        "tagtype": 3
      },
      {
        "tagcode": "golang",
        "tagname": "coding",
        "tagvalue": [
          "3"
        ],
        "tagtype": 3
      }
    ]
  }
}
`

    var model model
    json.unmarshal([]byte(json), &model)

    fmt.println(model.action)
    fmt.println(model.business.listid)

    fmt.println(model.business.objecttags)
}
Copy after login

You will get the output:

add
123
[{csharp codename [2]} {golang coding [3]}]
Copy after login

Here we take advantage of the fact that the json module will automatically map the key named tagcode to the structure field named tagcode, but in fact We should be clear:

type ObjectTagsList struct {
    TagCode  string   `json:"tagCode"`
    TagName  string   `json:"tagName"`
    TagValue []string `json:"tagValue"`
}
Copy after login

The above is the detailed content of Using struct to parse nested json in go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template