-1

Hi all I have a Json structure as below:

Json Structure: 

{
   "items": [
      {"pkg": "0", "pin": "0", "item": "0"},
      {"pkg": "1", "pin": "1", "item": "1"},
      {"pkg": "0", "pin": "2", "item": "2"},
      {"pkg": "1", "pin": "3", "item": "3"},
      {"pkg": "0", "pin": "4", "item": "4"},
      {"pkg": "1", "pin": "5", "item": "5"},
      {"pkg": "0", "pin": "6", "item": "6"},
      {"pkg": "1", "pin": "7", "item": "7"},
      {"pkg": "0", "pin": "8", "item": "8"},
      {"pkg": "1", "pin": "9", "item": "9"},
      {"pkg": "0", "pin": "10", "item": "10"},
      {"pkg": "1", "pin": "11", "item": "11"},
      {"pkg": "0", "pin": "12", "item": "12"},
      {"pkg": "1", "pin": "13", "item": "13"},
      {"pkg": "0", "pin": "14", "item": "14"},
      {"pkg": "1", "pin": "15", "item": "15"},
      {"pkg": "0", "pin": "16", "item": "16"},
      {"pkg": "1", "pin": "17", "item": "17"},
      {"pkg": "0", "pin": "18", "item": "18"},
      {"pkg": "1", "pin": "19", "item": "19"},
      {"pkg": "0", "pin": "20", "item": "20"},
      {"pkg": "1", "pin": "21", "item": "21"},
      {"pkg": "0", "pin": "22", "item": "22"},
      {"pkg": "1", "pin": "23", "item": "23"},
      {"pkg": "0", "pin": "24", "item": "24"},
      {"pkg": "1", "pin": "25", "item": "25"},
      {"pkg": "0", "pin": "26", "item": "26"},
      {"pkg": "1", "pin": "27", "item": "27"},
      {"pkg": "0", "pin": "28", "item": "28"},
      {"pkg": "1", "pin": "29", "item": "29"},
      {"pkg": "0", "pin": "30", "item": "30"},
      {"pkg": "1", "pin": "31", "item": "31"},
      {"pkg": "0", "pin": "0", "item": "32"},
      {"pkg": "1", "pin": "1", "item": "33"},
      {"pkg": "0", "pin": "2", "item": "34"},
      {"pkg": "1", "pin": "3", "item": "35"},
      {"pkg": "0", "pin": "4", "item": "36"},
      {"pkg": "1", "pin": "5", "item": "37"},
      {"pkg": "0", "pin": "6", "item": "38"},
      {"pkg": "1", "pin": "7", "item": "39"},
      {"pkg": "0", "pin": "8", "item": "40"},
      {"pkg": "1", "pin": "9", "item": "41"}
     ]
}

I have used the below Structure to store the values from json

package basics

import (
   "encoding/json"
   "fmt"
   "io/ioutil"
   "os"
   "strconv"
)
type NNodes struct {
    Items []NodeItem `json:"items"`
} 

type NodeItem struct {
    Pkg string `json:"pkg"`
    Pin string `json:"pin"`
    Item string `json:"item"`
}

func parser() {
     data, _ := ioutil.ReadFile("list.json")
     var result NNodes 
     var pkg, pin, item  int
     json.Unmarshal(data, &result)
     foobar := make(map[int]map[int][]int)
     siblings := make(map[int][]int)
     for _, value := range result.items {
         if pkg, err = strconv.Atoi(value.Pkg); err != nil {
            break 
         }
         if pin, err = strconv.Atoi(value.Pin); err != nil {
             break
         }
         if item, err = strconv.Atoi(value.Item); err != nil {
              break
         }
         siblings[pin] = append(siblings[pin], item)
         foobar[pkg] = siblings
    }
    fmt.Println(foobar)
} 
what i am getting is below output:

map[0:map[0:[0 32] 1:[1 33] 2:[2 34] 3:[3 35] 4:[4 36] 5:[5 37] 6:[6 38] 7:[7 39] 8:[8 40] 9:[9 41] 10:[10 42] 11:[11 43] 12:[12 44] 13:[13 45] 14:[14 46] .. so on 

what i am looking for is to have maps with unique pkg as below:

// Expected map output of foobar map.. basically there are 2 similar pin entries, and their corresponding item should be added to same slice.

 map[0:map[0:[0,32] 2:[2,34] 4[4:36] 6[6:38]... 
 map[1:map[1:[1,33] 3:[3,35] 5[5:37] 7[7:39]...

Any hints on where i am going wrong.

Niranjan M.R
  • 343
  • 1
  • 6
  • 23
  • You must export struct fields (e.g. rename `items` to `Items`). And always check errors in Go. – icza Jul 26 '22 at 10:10
  • @icza Thanks I have modified the program but not getting the result. I am trying to understand if my map definition is correct. – Niranjan M.R Jul 26 '22 at 10:38

1 Answers1

2

Was able to resolve the issue using the below code:

package basics

import (
   "encoding/json"
   "fmt"
   "io/ioutil"
   "os"
   "strconv"
)
type NNodes struct {
    Items []NodeItem `json:"items"`
} 

type NodeItem struct {
    Pkg string `json:"pkg"`
    Pin string `json:"pin"`
    Item string `json:"item"`
}

func parser() {
     data, _ := ioutil.ReadFile("list.json")
     var result NNodes 
     var pkg, pin, item  int
     json.Unmarshal(data, &result)
     foobar := make(map[int]map[int][]int)
     for _, value := range result.items {
         if pkg, err = strconv.Atoi(value.Pkg); err != nil {
            break 
         }
         if pin, err = strconv.Atoi(value.Pin); err != nil {
             break
         }
         if item, err = strconv.Atoi(value.Item); err != nil {
              break
         }
         if foobar[pkg] == nil {
             f[pkg] = make(map[int][]int)
             f[pkg][pin] = append(f[pkg][pin], item)
         else {
             f[pkg][pin] = append(f[pkg][pin], item) 
         }
    }
    fmt.Println(foobar)
} 
Niranjan M.R
  • 343
  • 1
  • 6
  • 23