Hi Everyone,
I'm following the ArcGIS API for Python docs in attempts to add a long list of fields to a Table.
The code example I'm trying to replicate from the docs is here below:
-----------------------------------------------------
>>> new_field = {
"fields": [
{
"name": "Loc Identifier",
"type": "esriFieldTypeString",
"alias": "safa",
"nullable": True,
"editable": True,
"length": 256,
}
]
}
>>> res = fl.manager.add_to_definition(
json_dict=add_field
)
>>> res
-----------------------------------------------------
My object of new fields which prints type of <class 'dict'> is shown here below:
-----------------------------------------------------
fields_to_add = []
for field in source_fields:
# Check if the field should be ignored or if it already exists
if field['name'].lower() not in fields_to_ignore_lower and field['name'].lower() not in target_field_names:
fields_to_add.append(field)
field_dict = {"fields":fields_to_add}
print(field_dict)
{'fields': [{ "name": "PropID", "type": "esriFieldTypeInteger", "alias": "PropID", "sqlType": "sqlTypeOther", "nullable": true, "editable": true, "domain": null, "defaultValue": null }, { "name": "SRLaneLot", "type": "esriFieldTypeString", "alias": "SRLaneLot", "sqlType": "sqlTypeOther", "length": 255, "nullable": true, "editable": true, "domain": null, "defaultValue": null }, { ... continues on with the rest of the fields ...
When I try to use the 'add_to_definition' method I get the following type error:
result = target_table.manager.add_to_definition(field_dict)
print(result)
TypeError: Object of type PropertyMap is not JSON serializable
As I type this out I realise the docs are showing the add_to_definition method for a type of FeatureLayer. Does anyone know how to add fields to a table that exists as a relationship class in ArcGIS Online?
Solved! Go to Solution.
The solution was right on front of me...
field_dict_list = []
for f in fields_to_add:
field_dict_list.append(dict(f))
I just needed to convert the type of PropertyMap to a dict, append to a list. Then use the list of dictionaries to update the table.
The solution was right on front of me...
field_dict_list = []
for f in fields_to_add:
field_dict_list.append(dict(f))
I just needed to convert the type of PropertyMap to a dict, append to a list. Then use the list of dictionaries to update the table.