问题
I'm using pandas.io.json.json_normalize()
to convert some json into a dataframe, which is then pushed to an SQLite database via df.to_sql()
.
However, I'm getting sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.
when progressing with this, I think due to one of my json fields being an empty array.
I understand I can pass additional path arguments to json_normalize
to have it pull out array values and augment the rows with the parent data:
json_normalize(json_data, 'field_name_of_empty_array_value')
However I can't use this for this instance, as the array is empty and so it returns no values.
Is there a way to set the empty list value to None
, or to serialize the empty list via the json_serialize
function? Or do I need to do this on the returned dataFrame
?
The reason I want to understand whether this is possible is to limit the complexity when dealing with more difficult json
which may have empty array values nested deep within its structure.
I've search for this specific question but can't seem to find an example dealing with this specific case, and not sure the docs covers it.
Thanks
回答1:
I believe this isn't possible. So instead, I'm using json_normalize
on the full json
object, then splitting as required using DataFrame
operations, before converting back to json
via df.to_json
, and using json_normalize
with different arguments - specifically, the record_path
argument to create records based on the array entries as required.
来源:https://stackoverflow.com/questions/50023807/using-pandas-io-json-json-normalize-with-empty-list-attributes