Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to resolve the error - Object of type bytes is not JSON serializable?

Asked by Dipesh Bhardwaj May 3, 2023 2.1K views 1 answer
Share

About this question

I am attempting to move ContentVersion records from one Salesforce org to another using the simple-salesforce python library.

I am querying the VersionData and returning it in r.content here:

import json import requests import base64 import chardet headers = { "Authorization": "OAuth " + source_sf.session_id, "Content-Type": "application/octet-stream" } for index, row in content_doc_source_data_df.iterrows(): r = requests.request('get', source_sf.base_url+'sobjects/ContentVersion/'+row['Id']+'/VersionData', headers=headers) content_doc_source_data_df.at[index, 'File Body'] = r.content break content_doc_source_data_df.drop('Id', axis=1, inplace=True) content_doc_source_data_df.drop('ContentDocumentId', axis=1, inplace=True) content_doc_source_data_df.drop('ContentLocation', axis=1, inplace=True) content_doc_source_data_df.drop('ContentUrl', axis=1, inplace=True) content_doc_source_data_df.drop('IsLatest', axis=1, inplace=True) content_doc_source_data_df.drop('VersionData', axis=1, inplace=True) content_doc_source_data_df = content_doc_source_data_df.rename(columns={'File Body': 'VersionData'}) display(content_doc_source_data_df)

Which I can tell is returning the bites data, which you can see here:

However, whenever I then attempt to resend it to the new org using the API I get the following error: "TypeError: Object of type bytes is not JSON serializable"

Here's the code for sending:

#INSERT FILE import ast content_doc_source_data = content_doc_source_data_df.to_dict('records') results = source_sf.bulk.ContentVersion.insert(content_doc_source_data, batch_size=10000,use_serial=True) display(results)


Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.