'Iterate over JSON with changing keys & parameterized MySQL query

I would like to iterate over a loaded json file with changing keys. The different json files all have the same structure but sometimes keys+values are added/nonexistend:

...
{
        "kind": "something",
        "data": {
          "created": Unix-Timestamp,
          "changed": Unix-Timestamp,
          "comment": "Comment",
          "author": "Le-Moi",
          "stuff": false,
...

I used:

json_objects = len(json_loaded['data']['children'])
for objects in range(json_objects):
                        try:
                            created = json_value['data']['created']
                        except:
                            created = 0
...

but there must be a "simpler" way to avoid getting errors on nonexisting keys+values....is there? Especially because there are around 120 keys in the json files I would like to import into a MySQL-Database.

Additionally i would like to use these values in a prepared parameterized SQL-query using the mysql.connector for python. Until now i did: insert_command = "INSERT INTO mytable (column1, column2) VALUES ('%s', %s)" and added the parameter with a python-list.

EDIT: I managed to create a dynamic query where the keys and values are added up by string-addition (thanks to Sravani S -> tutorialspoint.com/How-do-I-loop-through-a-JSON-file-with-multiple-keys-sub-keys-in-Python):

for (k, v) in json_loaded['data']['children'][k]['data'].items():
        keys += str(k) + ", "
        if type(v) is str:
                v = "'" + v + "'"
                values += str(v) + ", "
        keys = keys[:-2]
        values = values[:-2]
...

but now I am getting the following error: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[{'e': 'text', 't': 'Resource'}], ... caused by a dict value (?). Is it possible to insert that long "string" via my prepared query?



Solution 1:[1]

I managed to solve my problem in a kind of "dirty" way. First I tried to convert my cell-input to binary strings but that wasn't really expedient... My solution was to replace all escape characters using the xxx.replace("'", "`") function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DatWapiti