Kaan
Kaan

Reputation: 167

JSON nested structer golang

I am trying to create nats to mongodb adapter with golang and haven't figure out how to build a structer for this json

[
   [
      1580946420.000000,
      {
         "tag":"logger.example",
         "log":"2020-02-05T23:47:00+0000 INFO takes the value and converts it to string.",
         "container_id":"15fe6ca47aaa88d5d5a9936abaa1495e62d568eaede8a3860beda48e1404c0f5",
         "container_name":"/nats-mongo_log-generator_1",
         "source":"stdout"
      }
   ],
   [
      1580946423.000000,
      {
         "tag":"logger.example",
         "container_id":"15fe6ca47aaa88d5d5a9936abaa1495e62d568eaede8a3860beda48e1404c0f5",
         "container_name":"/nats-mongo_log-generator_1",
         "source":"stdout",
         "log":"2020-02-05T23:47:03+0000 INFO takes the value and converts it to string."
      }
   ],
  ...
]

Upvotes: 0

Views: 92

Answers (2)

Kaan
Kaan

Reputation: 167

Thanks for the tips :)

I finally solved this issue.

type CLog struct {
    Tag           string `json:"tag" bson:"tag"`
    Log           string `json:"log" bson:"log"`
    ContainerID   string `json:"container_id" bson:"container_id"`
    ContainerName string `json:"container_name" bson:"container_name"`
    Source        string `json:"source" bson:"source"`
}

var arr [][]json.RawMessage
        json.Unmarshal([]byte(string(m.Data)), &arr)

        // ? Loglar
        for _, arr1 := range arr {
            var logum CLog

            // ? Tek log
            dec := json.NewDecoder(bytes.NewReader(arr1[1]))
            dec.Decode(&logum)

            sugar.Debug(logum)
            _, err := collection.InsertOne(ctx, bson.D{})
            if err != nil {
                //sugar.Debug(err)
                //sugar.Debug(res)
            }
            // id := res.InsertedID
            // sugar.Debug(id)
        }

Upvotes: 0

Burak Serdar
Burak Serdar

Reputation: 51467

You can use a struct for the inner object:

type InnerObj struct {
   Tag string `json:"tag"`
   ContainerID string `json:"container_id"`
   ContainerName string `json:"container_name"`
   Source string `json:"stdout"`
   Log string `json:"log"`
}

The rest, you cannot do using structs.

To generate, you can build a tree using arrays, like this:

[]interface{} { []interface{} {1580946420.000000,InnerObj{...}},
{1580946423.000000, InnerObj{...}}}

Parsing this isn't easy using structs. You have a few options:

  • Unmarshal to interface{}, and work on it
  • Use a multi-pass approach

Multi-pass is easier to implement:

var arr [][]json.RawMessage
json.Unmarshal(data,&arr)

This should give you each inner array as a two-dimensional raw json message. Then you can use a decoder to work on each:

for _,arr1:=range arr {
   for _,arr2:=range arr1 {
     var data InnerObj
     var number float64
     dec:=json.NewDecoder(bytes.NewReader(msg))
     if msg[0]=='{' {
        dec.Decode(&data)
     } else {
        dec.Decode(&number)
     }
  }
}

You can, of course, use a Decoder all the way and parse the file yourself as well.

Upvotes: 2

Related Questions