Is it possible to use a Dictionary input field for custom fields?


Userlevel 1

This post was split from the topic: Documentation for The Dictionary Inputs 


Hi @ikbelkirasan,

I’m new with Zapier and trying to create an action that would accept custom fields using a Dictionary input field.

Is there a way that the values sent to the API could be formatted as such:

[“key 1: value 1”, “key 2: value 2”]

 

In the Code Mode, I added JSON.stringify(bundle.inputData.customFields), and it works when testing the api in the Developer.

But when testing this in the Zap itself, I get an The app returned "A JSONArray text must start with '[' at 1 [character 2 line 1]". error. Any ideas how I can go about this? 

Thank you so much in advance!


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

12 replies

Userlevel 7
Badge +12

Hi @trunas - Sure! Did you try something like this to format the payload data? The following code snippet assumes that the input dictionary is called dictionary.

const payload = Object.entries(bundle.inputData.dictionary).map(
([key, value]) => `${key}: ${value}`
);

 

Userlevel 1

Hi @ikbelkirasan 
I’m really sorry for the trouble, but it’s still throwing me an error when testing the Zap. 
This is my code when Configuring the API Request:

 

And this is the error being thrown in the Zap:

 

The API requires a string value so I used JSON.Stringify. but in the Zap it doesn’t seem to register the JSON.stringify method and seems to still be throwing an object. 

Am I doing this wrong? 

Again, I am very thankful for your advice. 

Userlevel 7
Badge +12

@trunas That’s because you aren’t actually sending the payload in the request. If you look closely, the payload is being set within the code that runs after the response is received. Instead, you’ll need to move the payload declaration to the top of the first line of your code and assign it to the body of the request. Can you copy and paste the code here so I can tweak it for you?

Userlevel 1

@ikbelkirasan I see, this is the current code that I have:

  params: {
'apiKey': bundle.authData.apiKey,
'subdomain': bundle.inputData.subdomain,
'studentId': bundle.inputData.studentId,
'customFields': JSON.stringify(bundle.inputData.customFields)
},
body: {

}
};

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

const payload = Object.entries(bundle.inputData.customFields).map(([key, value]) => `${key}: ${value}`);

return results;
});

 

So, it should be like this, is this correct? 

  params: {
'apiKey': bundle.authData.apiKey,
'subdomain': bundle.inputData.subdomain,
'studentId': bundle.inputData.studentId,
'customFields': JSON.stringify(bundle.inputData.customFields)
},
body: {

}
};

const payload = Object.entries(bundle.inputData.customFields).map(([key, value]) => `${key}: ${value}`);

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;


return results;
});

 

Userlevel 7
Badge +12

@trunas - Here’s how it should look like:

const payload = Object.entries(bundle.inputData.customFields).map(
([key, value]) => `${key}: ${value}`
);

const options = {
method: "POST",
url: "https://...",
params: {
apiKey: bundle.authData.apiKey,
subdomain: bundle.inputData.subdomain,
studentId: bundle.inputData.studentId,
},
body: payload,
};

return z.request(options).then((response) => {
response.throwForStatus();

const results = response.json;
return results;
});

 

Userlevel 1

That worked. Thank you so much! 

Just a quick question though, does that mean when I set a value as JSON.stringify() in the parameters, this won’t register for the zap? because I noticed that this happens for the fields that I add the JSON.stringify method too. 

It’s just a bit confusing when in the developer mode testing the api works but when I test it in the zap it throws an error. 

Userlevel 7
Badge +12

@trunas - Great! Glad to hear it!

Ok so based on your description of the problem, it sounds to me like you’re sending the payload from the developer mode in the correct format that the API expects. However, when you test within a zap, it doesn’t seem to be the case.

In order to understand what’s going wrong, you might want to go to the Monitoring section and look for both requests and compare them to spot the cause of this issue.

Userlevel 1

@ikbelkirasan I checked the monitoring, and in the development, the values being sent are correct

But in the Zap, it ends up sending the values in a different format

Why is it that the Zap isn’t sending the values the same way as when testing from the developer mode? 

Userlevel 7
Badge +12

@trunas - That’s weird! Can you post a screenshot of the fields configuration of this zap step?

Userlevel 1

@ikbelkirasan 

 

This is the api configuration in the developer mode

const options = {
url: 'https://...',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-KEY': bundle.authData.apiKey
},
params: {
'apiKey': bundle.authData.apiKey,
'subdomain': bundle.inputData.subdomain,
'studentId': bundle.inputData.studentId,
'customFields': JSON.stringify(bundle.inputData.customFields),
'fields': 'customFields'
},
body: {

}
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

// You can do any parsing you need for results here before returning them

return results;
});

This is the test data in developer mode:

 

This is the Action Setup in the Zap:

 

When I test the Zap, it shows as successful, but the data being sent is in a different format, so our API doesn’t handle it the way it’s supposed to. 

The Input field for the Custom Fields is set to a String.

 

Should I change the field type to a Text instead? In order for the values to be sent correctly?

Again, I apologize for the bother, since I’m new with Zapier I’m a bit confused as to why this is happening.

Userlevel 7
Badge +12

@trunas No worries!

So it turns out you’re using the wrong input field type here, you should change it to Dictionary to make it work as expected.

 

 

 

Edit: I thought you were using the code I posted before to convert the dictionary object to the desired format. I noticed that you’re sending “1: A, 2: B” from your zap whereas it should be [“1: A”, “2: B”]

Userlevel 1

@ikbelkirasan I see, would the Dictionary field word for fields (example: studentIds) that requires this value: [“1234”, “234”] as well? 
Since I have other actions with the same issue that I JSON.stringify for, but similar to the one above, that instead of this being sent as [“123”, “456”], the zap ends up sending it as “123, 456”.

 

I hope that explanation was understandable. :disappointed_relieved: