0

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."
      }
   ],
  ...
]

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i want to parse this json
0

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)
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.