APIs & Integrations

lucasrd
Member

Json parse error when calling import api endpoint using c# and .net

SOLVE

I am trying to post a csv file using the import API.  I followed an example here, but still am getting an "unable to parse json format" error when making the request.  Here is my code:

 

 

 

 

var key = ConfigurationManager.AppSettings["HSAPIKEY"];
                            var client = new RestClient(String.Format("https://api.hubapi.com/crm/v3/imports?hapikey={0}", key));

                            client.Timeout = -1;

                            var request = new RestRequest(Method.POST);
                            request.AddHeader("Content-Type", "multipart/form-data");
                            request.AddHeader("Cookie", "__cfduid=d09cae7faeb5960760427f1cf3f5bfd7a1597311822");

                            string payload = @"{
                          ""name"": ""test_import"",
                          ""files"": [
                            {
                              ""fileName"": ""hearing_first_test_2021-04-08.csv"",
                              ""fileFormat"": ""CSV"",
                              ""fileImportPage"": {
                                ""hasHeader"": true,
                                ""columnMappings"": [
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""email_address"",
                                    ""idColumnType"": ""HUBSPOT_ALTERNATE_ID"",
                                    ""propertyName"": ""email"",
                                    ""foreignKeyType"": null,
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  },
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""expected_due_date"",
                                    ""idColumnType"": ""null"",
                                    ""propertyName"": ""due_date"",
                                    ""foreignKeyType"": null,
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  },
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""first_name"",
                                    ""idColumnType"": ""null"",
                                    ""propertyName"": ""firstname"",
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  },
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""last_name"",
                                    ""idColumnType"": ""null"",
                                    ""propertyName"": ""lastname"",
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  },
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""zipcode"",
                                    ""idColumnType"": ""null"",
                                    ""propertyName"": ""zip"",
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  },
                                  {
                                    ""ignored"": false,
                                    ""columnName"": ""source_id"",
                                    ""idColumnType"": ""null"",
                                    ""propertyName"": ""bc_wte_contact"",
                                    ""columnObjectTypeId"":""0-1"",
                                    ""associationIdentifierColumn"": false
                                  }
                                ]
                              }
                                        }
                          ]
                        }";

                            request.AddParameter("importRequest", payload);
                            request.AddFile("files", AppDomain.CurrentDomain.BaseDirectory + "hearing_first_test_2021-04-08.csv");
                            request.RequestFormat = DataFormat.Json;

                            IRestResponse response = client.Execute(request);

 

 

 

The error is:

"{\"status\":\"error\",\"message\":\"Unable to process JSON\",\"correlationId\":\"d08507be-2b90-466e-99ec-1b6ab7fb76e9\"}"

 

The actual JSON generated that I'm posting is below and it validates in online JSON parsers.

 

 

 

{
                          "name": "test_import",
                          "files": [
                            {
                              "fileName": "hearing_first_test_2021-04-08.csv",
                              "fileFormat": "CSV",
                              "fileImportPage": {
                                "hasHeader": true,
                                "columnMappings": [
                                  {
                                    "ignored": false,
                                    "columnName": "email_address",
                                    "idColumnType": "HUBSPOT_ALTERNATE_ID",
                                    "propertyName": "email",
                                    "foreignKeyType": null,
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  },
                                  {
                                    "ignored": false,
                                    "columnName": "expected_due_date",
                                    "idColumnType": "null",
                                    "propertyName": "due_date",
                                    "foreignKeyType": null,
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  },
                                  {
                                    "ignored": false,
                                    "columnName": "first_name",
                                    "idColumnType": "null",
                                    "propertyName": "firstname",
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  },
                                  {
                                    "ignored": false,
                                    "columnName": "last_name",
                                    "idColumnType": "null",
                                    "propertyName": "lastname",
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  },
                                  {
                                    "ignored": false,
                                    "columnName": "zipcode",
                                    "idColumnType": "null",
                                    "propertyName": "zip",
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  },
                                  {
                                    "ignored": false,
                                    "columnName": "source_id",
                                    "idColumnType": "null",
                                    "propertyName": "bc_wte_contact",
                                    "columnObjectTypeId":"0-1",
                                    "associationIdentifierColumn": false
                                  }
                                ]
                              }
                                        }
                          ]
                        }

 

 

 

0 Upvotes
1 Accepted solution
lucasrd
Solution
Member

Json parse error when calling import api endpoint using c# and .net

SOLVE

I figured out the issue.  My JSON values which should have been passed as null were passed in as the string "null" and Hubspot didn't like that.

View solution in original post

5 Replies 5
RMones
Contributor | Platinum Partner
Contributor | Platinum Partner

Json parse error when calling import api endpoint using c# and .net

SOLVE

Hi @lucasrd ,

 

I think you've got this error because of the double double quotes around your strings.

""name"": ""test_import"",

Shoud be:

"name": "test_import",

 

You can check if your JSON is valid at https://jsonformatter.org/ . A nice tool that let you know where the error occours.

 

Regards Ronald

 

STurbadkar
Member

Json parse error when calling import api endpoint using c# and .net

SOLVE

What is the solution for this one? I am getting the same issue

0 Upvotes
lucasrd
Member

Json parse error when calling import api endpoint using c# and .net

SOLVE

@RMones - Thanks, but that is the escape sequence for a multi-line C# literal.  I have debugged this in Visual studio and the resulting JSON does parse correctly in an online parser.

0 Upvotes
lucasrd
Solution
Member

Json parse error when calling import api endpoint using c# and .net

SOLVE

I figured out the issue.  My JSON values which should have been passed as null were passed in as the string "null" and Hubspot didn't like that.

natsumimori
Community Manager
Community Manager

Json parse error when calling import api endpoint using c# and .net

SOLVE

Hi @lucasrd ,

 

Thank you for reaching out to the HubSpot Community.

@RMones and @Bryantworks , would you mind sharing your advice for @lucasrd ?

0 Upvotes