I am trying to write in a json file a specific string. My code for doing so is the following:
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client['my_db_values']
collection = db['db_values']
json_file = {"User": "2145", "Item": {"123456": {}}}
temp_json1 = {"timestamp": "2123532158", "process1_value": 0.4, "state": {"B": 0.1, "F": 0.2, "E": 0.3}}
temp_json2 = {"timestamp": "2323532158", "process2_value": 0.2, "P": 0.8}
json_file ["Item"][str(123456)]["process1"].append(temp_json1)
json_file ["Item"][str(123456)]["process2"].append(temp_json2)
Actually I have created the json file "json_file" and I want to add the sub-json for the process1 and process 2 which will be sub-categories of the "Item" field. My code does not work as I want and I am receiving the following error:
json_file ["Item"][str(213546879213)]["process1"].append(temp_json1)TypeError: 'set' object has no attribute 'getitem'
My json want to look like:
{
"User": "213546879546213",
"Item": {
"213546879": {
"Process1": [{
"Timestamp": "213546879213",
"Process1_Value": 0.4,
"state": {
"B": 0.2,
"F": 0.8,
"E": 0.1
}
}],
"Sla_Weight_Personalization": [{
"Timestamp": "213546879",
"Process2_Value": 0.4,
"P": 0.8
}]
}
}
}
{"123456"}that's a set. You probably want to use something else, here.