Parse JSON with several nested structs

(YoungGopher) #1

Hi all
I have the several structs in one format JSON

In one hard struct

type MainJSON []struct {
	NestedInMain []struct {
		Type       string `json:"type"`
		NestedInNested []struct {
			Type        string        `json:"type"`
			Points [][]float64   `json:"points"`
			Label       string        `json:"label"`
		} `json:"geometries"`
		Time  string `json:"time"`
	} `json:"paths"`
	Name        string        `json:"name"`
	Description string        `json:"description"`
	Rating      string        `json:"rating"`
	Prepared    bool          `json:"Prepared"`

I delimited her on several structs

  type MainJson []struct {
	NSIM NestedInMain
	Name        string        `json:"name"`
	Description string        `json:"description"`
	Rating      string        `json:"rating"`
	Prepared    bool          `json:"Prepared"`

type NestedInMain []struct {
	NIN NestedInNested
	Type string `json:"type"`
	Time string `json:"time"`

type NestedInNested []struct {
	Type   string      `json:"type"`
	Points [][]float64 `json:"points"`
	Label  string      `json:"label"`

So when i do something like this

iap := MainJson{}
err := json.Unmarshal([]byte(test), &iap)
if err != nil {

I can’t get access to data in first and second nested structures and they’s fields
How do this correct?
Later i want get json from response.body


(Bigpigeon) #2

NestedInMain field has json:"paths" tag you lose it

(YoungGopher) #3

I’m trying but this not work
This is mine json Example

        "First": [],
        "Nested": [
                "Type": "type",
                "Text": "text",
                "NestedSecond": [
                        "Type": "typetwo",
                        "Name": "nametwo",
                        "NestedThird": [
        "tags": [],
        "name": "someName"

When i do this code

package main

import (

type Test struct {
	First []interface{} `json:"First"`
	NS    []*Nested     `json:"Nested"`
	Tags  []interface{} `json:"tags"`
	Name  string        `json:"name"`

type Nested struct {
	Type string          `json:"Type"`
	Text string          `json:"Text"`
	NST   []*NestedSecond `json:"NestedSecond"`

type NestedSecond struct {
	Type        string      `json:"Type"`
	Name        string      `json:"Name"`
	NestedThird [][]float64 `json:"NestedThird"`

var nT *[]Test

func main() {
	file, _ := os.Open("test2.json")
	decoder := json.NewDecoder(file)

	err := decoder.Decode(&nT)
	if err != nil {


showed me
&[{[] [0xc420092080] [] someName}]

But i can’t get access

Returns me error
nT.NS undefined (type *[]Test has no field or method NS)

Finally I want to get NestedThird in two variables like a and b

a = 46.202634
b = 2.5699142

(Giulio Iotti) #4

The error says that your type doesn’t have that field. Your type is a pointer to a slice of Test structures.

You want to access one field inside the Test structure… but which one? Your JSON gives you many, thus the slice (remove the pointer to the slice, please).

Probably you need to cycle over every Test structure contained in your JSON and extract the values:


Should give you a pair of a, b as you expect.

(YoungGopher) #5

But how do it more simple?
Because JSON return me array of pairs - which i want send in go slice in the same order
For example JSON

  [1, 2],
  [3, 4],
  [5, 6]

(Norbert Melzer) #6

That JSON is not valid.

But if I simply remove the outer brackets, it’s a valid representation of [][2]int.

(system) #7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.