Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Created November 30, 2022 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevehenderson/e4283aeca51a8424e39d182fdb17deae to your computer and use it in GitHub Desktop.
Save stevehenderson/e4283aeca51a8424e39d182fdb17deae to your computer and use it in GitHub Desktop.
golang unmarshall nested json from result

How do you unmarshall nested json

Given dgraph res.Json:

"q": [
      {
        "city": {
          "uid": "0x51c7ebb",
          "name": "tsue",
          "postal_code": "0886"
        }
      },
      {
        "city": {
          "uid": "0x51c7ebb",
          "name": "tsue",
          "postal_code": "0886"
        }
      }]
type xRes struct {
		Q []struct {
			City struct {
				Uid        string `json:"uid"`
				Name       string `json:"name"`
				PostalCode string `json:"postal_code"`
			} `json:"city"`
		} `json:"q"`
	}

ures := xRes{}

err = json.Unmarshal(res.Json, &ures)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment