Search code examples
jsongosimplejson

how do you traverse a json file using go-simplejson


I have a JSON file of the form:

{
  "data": {
    "docs": [
      {"key00": "val00", "key01": "val01"},
      {"key10": "val10", "key11": "val11"}
    ]
  }
}

and I would like to convert it to separate JSON docs:

file0.json

{
  {"key00": "val00", "key01": "val01"}
}

file1.json

{
   {"key10": "val10", "key11": "val11"}
}

I can enumerate over the array contents using:

j, _ := ioutil.ReadFile(path)
dec, _ := simplejson.NewFromReader(bytes.NewReader(j))
for i,v := range dec.Get("data").Get("docs").MustArray() {
  out := simplejson.New()

  /* ??? move dec key/value pairs to out ??? */

  b, _ := out.EncodePretty()
  ioutil.WriteFile(outpath, b, 0777)
}

but I'm not sure how to iterate over the key/value pairs within the array entries. It's a nice, succinct library but there don't appear to be a lot of examples and my golang expertise is currently limited.

Any help would be appreciated.. thanks!


Solution

  • You can use simplejson.Set:

    for _, doc := range dec.Get("data").Get("docs").MustArray() {
        out := simplejson.New()
        // doc is an interface{} holding a map, we have to type assert it.
        for k, v := range doc.(map[string]interface{}) {
            out.Set(k, v)
        }
        b, _ := out.EncodePretty()
        fmt.Printf("%s\n", b)
    }
    

    However in that instance, simplejson is an overkill and using a struct / stdlib is more efficient.

    For completeness sake, the std lib version:

    type DataLayout struct {
        Data struct {
            Docs []map[string]string `json:"docs"`
        } `json:"data"`
    }
    
    func main() {
        var in DataLayout
        err := json.NewDecoder(strings.NewReader(j)).Decode(&in)
        if err != nil {
            log.Fatal(err)
        }
        for _, doc := range in.Data.Docs {
            b, err := json.MarshalIndent(doc, "", "\t")
            if err != nil {
                log.Fatal(err)
            }
            fmt.Printf("%s\n", b)
        }
    }
    

    play

    Notes:

    • Your json example is wrong, "key10", "val10" should be "key10": "val10".
    • When you're in doubt of how a data structure looks and too lazy to read the code, use fmt.Printf("%#v", doc) to see how it looks like.