function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Charles Kelly 17Charles Kelly 17 

Trying insert records via REST API POST request. Json File.

Hello,

I'm trying to insert the below json into a custom object named Player. I need to change the field names from amateur to amater__c and country to country__c etc. How can I do this at scale?

Also, it works if I insert one record at a time but gives the error below when there is multiple records trying to be inserted. 

Thank you

User-added image
Eden WheelerEden Wheeler
Hello! I'd be happy to help you with your JSON transformation and the error you're encountering.

To change the field names in your JSON at scale, you can use a programming language like Python or JavaScript to iterate through your JSON data and modify the field names. Here's an example using Python:
import json

def transform_json(json_data):
    transformed_data = []
    for record in json_data:
        transformed_record = {}
        for key, value in record.items():
            if key == "amateur":
                transformed_record["amateur__c"] = value
            elif key == "country":
                transformed_record["country__c"] = value
            # Add more field name mappings here if needed
            else:
                transformed_record[key] = value
        transformed_data.append(transformed_record)
    return transformed_data

# Example usage
json_data = [
    {
        "amateur": "Some Value",
        "country": "USA",
        "name": "John Doe"
    },
    {
        "amateur": "Another Value",
        "country": "Canada",
        "name": "Jane Smith"
    }
]

transformed_data = transform_json(json_data)
print(json.dumps(transformed_data, indent=2))
This code will iterate through each record in your JSON data and create a transformed record with the modified field names. You can add more field name mappings inside the transform_json function as needed.

As for the error you're encountering when trying to insert multiple records, it would be helpful if you could provide more details or the specific error message you're seeing. That way, I can assist you in resolving the issue more effectively.
 For more information, you can check this: azure admin training (https://www.igmguru.com/cloud-computing/microsoft-azure-administrator-az-103-certification-training/)

 
Charles Kelly 17Charles Kelly 17
Thank you Eden!

Do I put this code into the "pre-request script" tab?
Charles Kelly 17Charles Kelly 17
The error at the bottom of the screenshot is was I get when trying to insert multiple records:

[
{
"amateur__c": 0,
"country__c": "England",
"country_code__c": "ENG",
"dg_id__c": 14794,
"name": "Abbott, Jamie"
},
{
"amateur__c": 1,
"country__c": "Sweden",
"country_code__c": "SWE",
"dg_id__c": 23950,
"name": "Aberg, Ludvig"
}
]

returns an error of:

[
{
"message": "Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row",
"errorCode": "INVALID_FIELD"
}
]


Notice: I've changed all the field names to match SF so I don't know why it says "Invalid Field"...If I were to remove the second record and the [] then it works fine:
{
"amateur__c": 0,
"country__c": "England",
"country_code__c": "ENG",
"dg_id__c": 14794,
"name": "Abbott, Jamie"
}

Thank you!