I have a csv file with array in string format as below:
date,name,criteria
2018-05-16,John,"[{'age':35},{'birthyear':1983}]"
2018-05-16,Jane,"[{'age':36},{'birthyear':1982}]"
I am using Python with pandas and numpy for processing this
I need to import this file into MongoDB collection in following format :
{
"date":'2018-05-16',
"name":"John",
"criteria" : [
{"age":35},
{"birthyear" : 1983}
]
},
{
"date":'2018-05-16',
"name":"Jane",
"criteria" : [
{"age":36},
{"birthyear" : 1982}
]
}
`
I tried using json formatter , but after insertion into Mongodb I get the array to be same as in csv file.
I have tried following approaches:
#Approach 1
import pymongo
from pymongo import MongoClient
import pandas as pd
import numpy as np
import json
from datetime import datetime
df = pd.read_csv("file.csv")
records = json.loads(df.T.to_json()).values()
db.tmp_collection.insert_many(data.to_dict('record'))
#Approach 2
import pymongo
from pymongo import MongoClient
import pandas as pd
import numpy as np
import json
from datetime import datetime
df = pd.read_csv("file.csv")
data_json = json.loads(df.to_json(orient='records'))
db.tmp_collection.insert_many(data_json)
Both give following output in Mongodb collection :
{
"date" : "2018-05-16",
"name" : "John",
"criteria" : "[{age:35},{birthyear:1983}]"
}
Can you suggest some better way. P.S. i am new to Python.
Thanks in advance.