5

I try to convert to node.js code into golang code.

This is my sample JSON.

{
  "result": {
    "birthInfo": {
      "birthYmd": "2018-07-25",
      "cattleNo": "cow001",
      "docType": "registerBirth",
      "lsTypeNm": "redbull",
      "monthDiff": "2018-07",
      "nationNm": "japan",
      "regType": "directly",
      "regYmd": "2018-07-25",
      "sexNm": "farm001"
    },
    "breed": {
      "dead": {
        "deadCd": "deadcd20180725",
        "deadYmd": "2018-07-25",
        "docType": "reportDeCattle"
      },
      "earTag": {
        "docType": "reattachEartag",
        "flatEartagNo": "eartag206015",
        "rfidNo": "rfid234234"
      }
    }
  }
}  

When using node.js, it is easy to get or access json data like this.

let cowbytes = await stub.getState("cow001");
var cowInfo = JSON.parse(cowbytes);

var eartag = {
  docType: 'reattachEartag',
  flatEartagNo: "eartag206015",
  rfidNo: "rfid234234",
};

if (cowInfo.breed) {
  cowInfo.breed['earTag'] = eartag;
} else {
  cowInfo.breed = {
    earTag: eartag
  };
}

await stub.putState(args[0], Buffer.from(JSON.stringify(cowInfo)));

And this is my golang code which I benchmark node.js code.

cowbytes, _: = APIstub.GetState("cow001")
if cowbytes == nil {
  return shim.Error("Incorrect value. No cattle Info..")
}

var cowInfo map[string] interface {}
json.Unmarshal(cowbytes, & cowInfo)
eartag: = make(map[string] interface {})

eartag["docType"] = "reattachEartag"
eartag["flatEartagNo"] = string("eartag206015")
eartag["rfidNo"] = string("rfid234234")


_, exists: = cowInfo["breed"]
if exists {
  inputJSON,
  err: = json.Marshal(cowInfo["breed"])
  fmt.Println(err)
  out: = map[string] interface {} {}
  json.Unmarshal([] byte(inputJSON), & out)
  out["earTag"] = struct {
    EarTag map[string] interface {}
    `json:"earTag"`
  } {
    eartag,
  }
  cowInfo["breed"] = out

}
else {
  cowInfo["breed"] = struct {
    EarTag map[string] interface {}
    `json:"earTag"`
  } {
    eartag,
  }
}

cattleAsBytes, _: = json.Marshal(cowInfo)
APIstub.PutState(string("cow001"), cattleAsBytes)    

Although my golang file works fine, I think it is not only hard to write code but also performane is bad(duplicate marshal & unmarshal).

how can I easily control JSON type in Golang.

Does anyone have an idea?

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
TH Cho
  • 61
  • 5
  • "how can I easily control JSON type in Golang." by not using `map[string]interface{}` and instead using a concrete struct type that matches the json in its structure. When converting node to Go don't expect to only get benefits from it, some things you gain, some you loose. – mkopriva Aug 03 '18 at 09:09

2 Answers2

4

You usually have to choose between performance and simplicity.

If you need performance, then model your JSON input with structs, and then you can easily get/set different parts using selectors on your structs, and you also get Go's type safety for free. Note: there are libs and online services which automatically generate Go structs from input JSON text, such as https://mholt.github.io/json-to-go/.

If you choose simplicity, you may use 3rd party libs that allow easy manipulation of "dynamic" objects (e.g. maps and slices which are the result of unmarshaling such JSON input text).

For one, there's github.com/icza/dyno (disclosure: I'm the author).

Using dyno, the simplicity of the solution is close to that of JavaScript's. Since dyno operates on maps and slices, it may be slower for certain tasks, but for such simple one that is in your question, it may be even faster because it does not use reflection (only type assertion and type switches).

Let's say the identifier src holds your input JSON text (may be a constant or a variable), then the Go code equivalent to yours is:

var cowInfo interface{}
if err := json.Unmarshal([]byte(src), &cowInfo); err != nil {
    panic(err)
}

eartag := map[string]string{
    "docType":      "reattachEartag",
    "flatEartagNo": "eartag206015",
    "rfidNo":       "rfid234234",
}

if breed, err := dyno.Get(cowInfo, "result", "breed"); err == nil {
    dyno.Set(breed, eartag, "earTag")
} else {
    dyno.Set(cowInfo, map[string]interface{}{"earTag": eartag}, "result", "breed")
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
if err := enc.Encode(cowInfo); err != nil {
    panic(err)
}

The above code unmarshals the input JSON, performs the processing / modification, and finally it marshals the result onto your console.

Here's the complete app: Go Playground. Note: you can't run it on the Go Playground because it imports github.com/icza/dyno (and importing 3rd party libs is not allowed there), you have to run it locally (after go getting dyno).

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thanks, I tried to use your code and this is what I want. I extracted your GetSet method in your package in go playground, it runs as well. playground(https://play.golang.org/p/7WkOFem5dkq) – TH Cho Aug 06 '18 at 01:31
  • I have another question, node.js has another function like .length so, it is easy to get how many tags in JSON struct. For example, when I want to know how many 'send' tag in my JSON struct, you just type `var howManySend = cowInfo.breed.move.send.length;`. Does your package has a kind of this function? – TH Cho Aug 06 '18 at 04:18
  • @조태희 You can do that by getting the `"send"` value as a map with `dyno.GetMapS(cowInfo, "result", "breed", "move", "send")`, and call the builtin `len()` function on the return value (if there are no errors). – icza Aug 06 '18 at 06:10
  • Is it available when the inside JSON data type is string? Here is my JSON type when I marshal my JSON Type. { "birthInfo":{ "birthYmd":"2018-07-25", "cattleNo":"cow001", "docType":"registerBirth", }, "breed":{ "move":{ "send":[ { "agencyCd":"agency001", "agencyNm":"S1", "docType":"reportTransferSnd", "no":0, "receiveFarmNo":"farm002", "sendFarmNo":"farm001", } ] } } } – TH Cho Aug 06 '18 at 06:29
  • 1
    @조태희 In the JSON you posted here, `"send"` is a JSON array. So in this case use `dyno.GetSlice(cowInfo, "result", "breed", "move", "send")`, which when succeeds, returns you a slice and you can again use the builtin `len()` function to get how many elements it has. – icza Aug 06 '18 at 06:31
  • Can I check if there is data or not? in node.js, it can write, ` if(cowInfo.breed.move){ if(cowInfo.breed.move.send){ var serialNum = cowInfo.breed.move.send.length; }else{ var serialNum = 0; } }else{ var serialNum = 0; } ` – TH Cho Aug 06 '18 at 06:41
  • 1
    @조태희 `GetXXX()` functions of `dyno` return you also an error. You may check that error value, you may also check if the reutrned value is `nil`. – icza Aug 06 '18 at 06:42
  • How can I add specific JSON Array? Let's say in Node.js, I'd liket to add data into array[2]. `cowInfo.breed.move.send[2] = snd` – TH Cho Aug 06 '18 at 08:30
  • @THCho You may use the `Append()` or `AppendMore()` functions, e.g. `dyno.Append(cowInfo, snd, "result", "breed", "move", "send")`. But please read the doc and do some research before asking any of the functionalities of `dyno`: https://godoc.org/github.com/icza/dyno – icza Aug 06 '18 at 08:43
  • I'm sorry, I don't know how I can touch with you. I use your package very well. but here is question. Let's say here is my json. `"processing":{ "process":[ { "processPlaceAddr":"", "processPlaceCd":"M108403", "processPlaceNm":"Chongchaeborihanwoo supply center" } ] }` If I want to get just value which is inside "processPlaceCd". How can I handle this? – TH Cho Sep 03 '18 at 09:11
  • I try to get method, getSlice method and getString. it doesn't work. Here is what I do. `test2, _ := dGet(cowInfo, "processing", "process", "processPlaceCd") dSet(cowInfo, map[string]interface{}{"sibal": test2}, "docType")` Please let me know, if you know this.. Thanks. – TH Cho Sep 03 '18 at 09:14
  • 1
    @THCho This will do it: `v, err := dyno.Get(m, "processing", "process", 0, "processPlaceCd")`. If you print the result values with `fmt.Println(v, err)`, output will be the expected `M108403 `. – icza Sep 03 '18 at 10:37
0

If your json structure is static I recommend to define appropriate type like:

type Result struct {
   BirthInfo struct {
       birthYmd string `json:"birthYmd"`
       // other fields go here
   } `json:"birthInfo"
   // ...
} `json:"result"`

then parse it once and use:

cowInfo.BirthInfo.CattleId
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59