3

My json looks like the following,

[
  {
    "key1": 1,
    "key2": "val2"
  },
  {
    "key1": 2,
    "key2": "val2"
  }
]

This json comes in string format and I want the objects in the json array to be inserted as individual records in mongodb. I referred to https://labix.org/mgo but wasn't able to find enough examples on the above use-case. Appreciate your thoughts in finding a solution.

1
  • 2
    Well, what have you tried so far? You must have at least some code. However bad it may be - we were all beginners once. Commented Dec 24, 2015 at 6:45

3 Answers 3

11

Unmarshal the JSON to []interface{} and insert the result in the database. Assuming that c is an mgo.Collection and data is a []byte containing the JSON value, use the following code:

var v []interface{}
if err := json.Unmarshal(data, &v); err != nil {
   // handle error
}
if err := c.Insert(v...); err != nil {
   // handle error
}
Sign up to request clarification or add additional context in comments.

Comments

2

In this example I will store the mixed array

test_string := '[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]'

inside the mongodb as the json:

{datum: [[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]}

package main

import (
    "strings"
    "context"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
)



type datum2 struct {
    Datum interface{} `json:datum`
}





var userCollection = db().Database("goTest").Collection("users") // get collection "users" from db() which returns *mongo.Client

func typeinterface2mongo() {
    
    
    var datum2 datum2_instance
    var interfacevalue []interface{}
    
    test_string := `[[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"\"x","[y","'z",[[1,2,3],[1,2,3]]]]`
    if err := json.Unmarshal([]byte(test_string), &interfacevalue); err != nil {
        fmt.Println(err)
        return
    }
    
    fmt.Println(test_string)
    fmt.Println(interfacevalue)
    datum2_instance.Datum=interfacevalue
    userCollection.InsertOne(context.TODO(), datum2_instance)
    fmt.Println(datum2_instance)
    fmt.Println(datum2_instance.Datum)  
}


Comments

1

If you have json data already please follow from step 2.

If you have Xml data first you need to convert to the json format by using this package("github.com/basgys/goxml2json")

 type JsonFileResponse struct {
    JsonData string        `bson:"JsonData " json:"JsonData"`
}

step 1: jsonData, err := xml2json.Convert(xml)
        if err != nil {
            panic("getting error while converting xml to json",err)
        }

step 2: session need to open by using your mongodb credentials.

    collection := session.DB("database name").C("Collection Name")
    err = collection.Insert(JsonFileResponse{JsonData :json.String()})
    if err != nil {
        log.Fatal(err)
    } 

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.