如何使用Go在嵌套JSON中进行多步搜索

I am developing a website using Go for back-end and Angular for front-end. In Go, I fetch raw data from database and reference fixed setting table(JSON formatted) then overwrite to corresponding columns.

Raw data look like this:

 Site  Code           Main
       0700-Shift     010_A
       2135-Packing   030_C
       3343-Check     050_E
       4355-Casting   080_H
       6903-ReDo Test 020_B
       2277-Scope chk 040_D

I cut some part of setting table:

[{"010_A": [
{
  "Code1": "010_01",
  "Code2": "",
  "Seq": "000 Start",
},
{
  "Code1": "010_07",
  "Code2": "010_0700",
  "Seq": "010 Shift"
},
],
"020_B": [{
  "Code1": "020_69",
  "Code2": "",
  "Seq": "000 ReDo Test"
},
{
  "Code1": "020_27",
  "Code2": "",
  "Seq": "000 Redo Combine"
}
],
"080_H": [
{
  "Code1": "080_06",
  "Code2": "",
  "Seq": "005 Merge"
},
{
  "Code1": "080_43",
  "Code2": "",
  "Seq": "010 Casting"
},
{
  "Code1": "080_66",
  "Code2": "080_6621",
  "Seq": "100 Cooling"
}
]}]

And the Go struct for setting table is:

type Settingtable struct {
    Code1    string
    Code2    string
    Seq      string
}

I using "Main" to check setting table "010_A" to "080_H" first, if match then use first 4 digits of "Code" to check "Code2" in setting table. If "Main" and "Code2" all match then return "Seq" and paste to "Site" column.

The following is where I stuck in:

package main

import (
"encoding/json"
"fmt"
)

func main() {
    var jsonBlob = []byte(`[
        {"010_A": [
        {
            "Code1": "010_02",
            "Code2": "010_0231",
            "Seq": "000 Start"
        },
        {
            "Code1": "010_08",
            "OP_CODE": "010_0822",
            "Seq": "010 Shift"
        }
        ],
         "020_B": [{
            "Code1": "020_69",
            "Code2": "020_7011",
            "Seq": "000 ReDo Test"
       },
       {
            "Code1": "020_27",
            "Code2": "",
            "Seq": "000 Redo Combine"
       }
       ],
        "080_H": [
       {
            "Code1": "080_06",
            "Code2": "",
            "Seq": "005 Merge"
       },
       {
            "Code1": "080_43",
            "Code2": "",
            "Seq": "010 Casting"
       },
       {
            "Code1": "080_66",
            "Code2": "080_6621",
            "Seq": "100 Cooling"
       }
       ]}
       ]`)

type Record map[string][]map[string]string
var records []Record

err := json.Unmarshal(jsonBlob, &records)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%+v
", records)

if v, s := records[0]["020_B"][0]["Code2"]; s {
    fmt.Println("ok:", v)
    }
}

It only return first result not to mention what the next function I want to do (loop input search condition, paste to raw data column..)

I'm not sure I understood properly what you want, so I wrote a snippet :

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Setting struct {
    Code1 string
    Code2 string
    Seq   string
}

type entry struct {
    site string
    code string
    main string
}

func main() {
    entries := []entry{
        {"", "0700-Shift", "010_A"},
        {"", "2135-Packing", "030_C"},
        {"", "3343-Check", "050_E"},
        {"", "4355-Casting", "080_H"},
        {"", "6903-ReDo Test", "020_B"},
        {"", "2277-Scope chk", "080_H"},
    }

    var jsonBlob = []byte(`[
        {"010_A": [
        {
            "Code1": "010_02",
            "Code2": "010_0231",
            "Seq": "000 Start"
        },
        {
            "Code1": "010_08",
            "OP_CODE": "010_0822",
            "Seq": "010 Shift"
        }
        ],
         "020_B": [{
            "Code1": "020_69",
            "Code2": "020_7011",
            "Seq": "000 ReDo Test"
       },
       {
            "Code1": "020_27",
            "Code2": "",
            "Seq": "000 Redo Combine"
       }
       ],
        "080_H": [
       {
            "Code1": "080_06",
            "Code2": "",
            "Seq": "005 Merge"
       },
       {
            "Code1": "080_43",
            "Code2": "",
            "Seq": "010 Casting"
       },
       {
            "Code1": "080_66",
            "Code2": "080_6621",
            "Seq": "100 Cooling"
       },
       {
          "Code1": "080_66",
          "Code2": "2277",
          "Seq": "Test"
       }
       ]}
       ]`)

    datas := []map[string][]Setting{}

    if err := json.Unmarshal(jsonBlob, &datas); err != nil {
        log.Fatal(err)
    }

    for key, settings := range datas[0] {
        for _, setting := range settings {
            for k, e := range entries {
                if e.main == key && setting.Code2 == e.code[:4] {
                    entries[k].site = setting.Seq
                }
            }
        }
    }

    fmt.Println(entries)
}