1

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

 }
}
2
  • {"123456"} that's a set. You probably want to use something else, here. Commented Apr 26, 2017 at 18:42
  • Sup dawg, I heard you like JSON. Commented Apr 26, 2017 at 18:43

1 Answer 1

4

In the line

json_file = {"User": "2145", "Item": {"123456"}}

... you are creating the Item entry as the set {"123456"} containing a single entry, but you must initialize it as a dict. You probably want to initialize it as {"123456": {}} or {} (an empty dict) instead.

Note that Python syntax on dict and set literals can be a bit confusing: {x} is a set, {x: y} is a dict, and {} is an empty dict (use set() for the empty set).


In order for the two append lines last in your code to work, you need to initialize the "123456" to be a dict containing empty "process1" and "process2" lists, as follows:

json_file = {
    "User": "2145",
    "Item": {
        "123456": {
            "process1": [],
            "process2": [],
        },
    },
}
Sign up to request clarification or add additional context in comments.

6 Comments

This valus it is a id of the item i just want to have a lot of values with a sub-list inside with properties.
Using the syntax {"123456"} in the context of JSON is definitely wrong, since {"123456"} is not valid JSON. Use {"123456": {}} instead or something similar that's appropriate for your use-case.
So sth like that json_file = {"User": "2145", "Item": {"123456": {}}}?
Well, try it out and let me know if it works. That's the only way to be sure.
Tried json_file = {"User": "2145", "Item": {"123456": {}}} and I got KeyError: 'process1' from the line ->json_file ["Item"][str(123456)]["process1"].append(temp_json1)
|

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.