python 3.x - How do I combine the object produced by selecting a JSON column into a JSON object proper? -
using psycopg , built-in json library, have python code looks this:
cur = connection.cursor() cur.execute('select * my_postgresql_table;') records = cur.fetchall() cur.close() record in records: my_json_column, other_column = record dict_for_json = { starting_information: other_column } json_i_want = json.dumps(some magical code combines dict_for_json , my_json_column 1 big dictionary) do_what_i_ultimately_want_to_do(json_i_want)
what want "magical" line? think want take value psycopg gives me when retreives json-types column, convert dictionary, , merge dictionary created. can json.dumps()
final dictionary json object proper.
i understand can use .update()
merge 2 dictionaries (how merge 2 dictionaries in single expression?). , according psycopg docs,
reading database, json , jsonb values automatically converted python objects.
so think able convert my_json_column
dictionary usual way, update
dict_for_json
. when try that:
json_i_want = json.dumps(dict_for_json.update(my_json_column.__dict__))
i error:
json_i_want = json.dumps(dict_for_json.update(my_json_column.__dict__)) attributeerror: 'dict' object has no attribute '__dict__'
what should doing merge 2 dictionaries?
to clear, when want merge 2 dictionaries, mean want new dictionary has of key-value pairs both existing dictionaries. if existing dictionaries both share key different values, doesn't matter me value clobbered in merge in situation i'm working with.
Comments
Post a Comment