简体   繁体   中英

How to replace json values from key-value map in GOlang

I have below json & I need to replace employee_names to emp_id from a map.

Tried this GolangPlay , but not sure how to replace values from the given map and error handling if the value is not present in given map.

Json data:

 [ { "dept": "IT", "condition": { "employee": [ "emp1" ] } }, { "dept": "HR", "condition": { "employee": [ "emp2", "emp3" ] } } ]

Map data:

[{emp1 14325} {emp3 49184} {emp2 21518}]

Expected output:

 [ { "dept": "IT", "condition": { "employee": [ 14325 ] } }, { "dept": "HR", "condition": { "employee": [ 21518, 49184 ] } } ]

code :

Started with below code , but not sure how to use the given map to replace with error handling.

    package main

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

func main() {

    jsonStr := `[
      {
        "dept": "IT",
        "condition": {
          "employee": [
            "emp1"
          ]
        }
      },
      {
        "dept": "HR",
        "condition": {
          "employee": [
            "emp2",
            "emp3"
          ]
        }
      }
    ]`

    empMap := `[{emp1 14325} {emp3 49184} {emp2 21518}]`

    type GetEmployee []struct {
        Dept      string `json:"dept"`
        Condition struct {
            Employee []string `json:"employee"`
        } `json:"condition"`
    }

    var empResponse GetEmployee
    unmarshallingError := json.Unmarshal([]byte(string(jsonStr)), &empResponse)
    if unmarshallingError != nil {
        fmt.Println(unmarshallingError.Error())
    }
    fmt.Println(empResponse)
    fmt.Println(empMap)

    for i := range empResponse {
        fmt.Println(i)
    }

}

Instead of storing the ids in an array of {ids: value} , it will be better for them to be in a map instead.

The above will range over the Employees' name and change it to the id. A check is made to see if there is a certain id key in the map.

for i, e := range empResponse {
        fmt.Println(e)
        for j,val := range empResponse[i].Condition.Employee {
          if _, ok := ids[val]; ok {
            empResponse[i].Condition.Employee[j] = ids[val]
          }
        }
    }

Full code

package main

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

func main() {

    jsonStr := `[
      {
        "dept": "IT",
        "condition": {
          "employee": [
            "emp1"
          ]
        }
      },
      {
        "dept": "HR",
        "condition": {
          "employee": [
            "emp2",
            "emp3"
          ]
        }
      }
    ]`

    empMap := `{"emp1": "14325", "emp3": "49184", "emp2": "21518"}`

    type GetEmployee []struct {
        Dept      string `json:"dept"`
        Condition struct {
            Employee []string `json:"employee"`
        } `json:"condition"`
    }

    var empResponse GetEmployee
    var ids map[string]string
    unmarshallingError := json.Unmarshal([]byte(string(jsonStr)), &empResponse)
    if unmarshallingError != nil {
        fmt.Println(unmarshallingError.Error())
    }
    json.Unmarshal([]byte(empMap), &ids)
    fmt.Println(empResponse)
    fmt.Println(ids)

    for i, e := range empResponse {
        fmt.Println(e)
        for j,val := range empResponse[i].Condition.Employee {
          if _, ok := ids[val]; ok {
            empResponse[i].Condition.Employee[j] = ids[val]
          }
        }
    }
    
    fmt.Println(empResponse)

}

playground

In the above, the id are strings since the name to be replaced are strings. Actually Employee are of type []string . If a string is to be replaced by an int, then Employee type needs to be changed to []interface{} .

playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM