Context object not JSON serialisable

I’m facing a weird issue,

my script does several publishes using sgtk.util.register_publish() and then creates a version and links the various publishes to this version.

I have a context created with context_from_entity.

The register_publish functions work just fine and create publishes linked to my context.
The sg.create(“Version”, data) function errors out on the same context opbject with the following error:

TypeError: is not JSON serializable


This is the full log:

Traceback (most recent call last):
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\app_store\tk-multi-publish2\v2.5.0\python\tk_multi_publish2\api\plugins\”, line 280, in _handle_plugin_error
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\app_store\tk-multi-publish2\v2.5.0\python\tk_multi_publish2\api\plugins\”, line 196, in run_publish
self._hook_instance.publish(settings, item)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\config\hooks\tk-multi-publish2\tk-nuke\editorial\ingest_tool\”, line 387, in publish
sg_version = sg.create(“Version”, data)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\”, line 1363, in create
record = self._call_rpc(“create”, params, first=True)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank\authentication\”, line 63, in _call_rpc
return super(ShotgunWrapper, self)._call_rpc(*args, **kwargs)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\”, line 3291, in _call_rpc
encoded_payload = self._encode_payload(payload)
File “G:\Projects__SGToolkitMasterConfig_configs\shotgun__SGToolkitMasterConfig-sgtk-beta\install\core\python\tank_vendor\shotgun_api3\”, line 3417, in encode_payload
wire = json.dumps(payload, ensure_ascii=False)
File "C:\Program Files\Nuke11.3v6\lib\json_init
.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File “C:\Program Files\Nuke11.3v6\lib\json\”, line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File “C:\Program Files\Nuke11.3v6\lib\json\”, line 270, in iterencode
return _iterencode(o, 0)
File “C:\Program Files\Nuke11.3v6\lib\json\”, line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: is not JSON serializable

1 Like

Weird, the type name is empty, the error should be <type> is not JSON serializable.
What sort of objects does the payload contain?

1 Like

Just a dictionary of normal version keys:

“project”: item.context.project,
“description”: item.description,

1 Like

It’s a bit confusing that the api is used differently in the collector and the publisher, I often have to go back and forth to verify I’m using the right syntax.

Should that be["framerange"].value instead?

You could try serializing individual objects before this step to see which one breaks it.

1 Like

No I know it works because if I comment out the


the version creation is happy and works fine.

The context object was created using tk.context_from_entity() and it worked fine in the register_publish function before the version creation.

1 Like

Oh sorry, I missed this in your post, but yes, I see now.
I don’t think contexts are serializable, actually. register_publish might be doing some additional conversion.


Right I solved it.

I have to create a context object (context_from_entity) for the register_publish and a simple dict context for the api sg.create.

It’s a bit confusing indeed to figure out when to use which.

1 Like

Glad it worked. Just to be sure, there is the context.to_dict() function, which you could use.


Oh thats nice!

I’ll use that!

1 Like