-1

user golang , write json unmarshal, error happened, Because of comma before "]".

import (
    "encoding/json"

    "github.com/c2h5oh/datasize"
    xdsboot "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2"
    "github.com/golang/protobuf/jsonpb"
)

err = json.Unmarshal(content, cfg)
    if err != nil {
        log.StartLogger.Fatalf("[config] [default load] json unmarshal config failed, error: %v", err)
    }
    return cfg

return error:

2021-07-14 06:53:39,637 [FATAL] [config] [default load] json unmarshal config failed, error: invalid character ']' looking for beginning of value

I use unit test case to run, find file input

func TestMosnEnvoyMode(t *testing.T) {

    content, _ := ioutil.ReadFile("./test.json") // this file is input file
    cfg := &MOSNConfig{}
    if err := json.Unmarshal([]byte(content), cfg); err != nil {
        t.Fatal(err)
    }
    if cfg.Mode() != Mix {
        t.Fatalf("config mode is %d", cfg.Mode())
    }
}

file content:

{
  

    "stats_matcher": {
      "inclusion_list": {
        "patterns": [
         
          {
            "prefix": "cluster.xds-grpc"
          },
          {
            "suffix": "ssl_context_update_by_sds"
          },   // the error cause by here, ","
        ]
      }
    }
  },

}

if I delete comma here

 {
            "suffix": "ssl_context_update_by_sds"
          },   // the error cause by here, ","

It is OK!

Now, why and which json lib should I use? Because the input file can not change.

user3373790
  • 39
  • 1
  • 5

1 Answers1

2

It's better if you can fix the JSON as it's invalid JSON to have trailing commas.

That being said, some languages support trailing commas natively, notably JavaScript, so you may see it in your data.

If you cannot change your data, switch to a JSON parser that supports trailing commas like HuJSON (aka Human JSON) which supports trailing commas and comments in JSON. It's a soft fork of encoding/json and the last 3 commits are from noted Xoogler and Ex-Golang team member Brad Fitzpatrick.

The Unmarshal syntax is the same as encoding/json, just use:

err := hujson.Unmarshal(data, v)

I've used it and it works as described.

Grokify
  • 15,092
  • 6
  • 60
  • 81