Skip to main content
Best answer

Dynamically list out fields from JSON returned from my API call

  • June 11, 2021
  • 1 reply
  • 160 views

{
    "entity": "ClientCorporation",
    "entityMetaUrl": "https://xxx.com?fields=*",
    "label": "Company",
    "dateLastModified": "1623389646848",
    "fields": [
        {
            "name": "id",
            "type": "ID",
            "dataType": "Integer"
        },
        {
            "name": "address",
            "type": "COMPOSITE",
            "dataType": "Address",
            "confidential": false,
            "label": "Address",
            "hideFromSearch": false,
            "fields": [
                {
                    "name": "address1",
                    "type": "SCALAR",
                    "dataType": "String",
                    "maxLength": 100,
                    "confidential": false,
                    "label": "Main Address",
                    "hideFromSearch": false
                },
                {
                    "name": "address2",
                    "type": "SCALAR",
                    "dataType": "String",
                    "maxLength": 100,
                    "confidential": false,
                    "label": "Address2",
                    "hideFromSearch": false
                },
                {
                    "name": "city",
                    "type": "SCALAR",
                    "dataType": "String",
                    "maxLength": 40,
                    "confidential": false,
                    "label": "City",
                    "hideFromSearch": false
                },
                {
                    "name": "countryCode",
                    "type": "SCALAR",
                    "dataType": "String",
                    "maxLength": 0
                },
                {
                    "name": "countryID",
                    "type": "SCALAR",
                    "dataType": "Integer",
                    "confidential": false,
                    "label": "Country",
                    "optionsType": "Country",
                    "optionsUrl": "https://rest21.bullhornstaffing.com/rest-services/7i8ptd/options/Country",
                    "hideFromSearch": false
                },
                {
                    "name": "countryName",
                    "type": "SCALAR",
                    "dataType": "String",
                    "maxLength": 0
                },

I am trying to dynamically list out fields from JSON returned from my API call. The name I need are in the “name” section of the JSON and as you can see there are situations like “address” that also has sub fields. I want to be able to list them all out. 

 

I am trying to make this section of the code work:

 

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

    // modify your api response to return an array of Field objects
    // see https://zapier.github.io/zapier-platform-schema/build/schema.html#fieldschema
    // for schema definition.

    return results;
  });

 

 

Best answer by ZaneBest answer by Zane

You’ll need to write some JavaScript transform that object into a flat array of Field objects as defined by the Zapier Platform schema: https://zapier.github.io/zapier-platform-schema/build/schema.html#fieldschema

 

To surface those nested fields you might have to map through those recursively and give them “namespaced” keys (e.g. “address~~city”) so you can do the transform in reverse to get them back into the correct structure before sending the data to your API.  

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Forum|alt.badge.img+9
  • Zapier Staff
  • 331 replies
  • Answer
  • June 14, 2021

You’ll need to write some JavaScript transform that object into a flat array of Field objects as defined by the Zapier Platform schema: https://zapier.github.io/zapier-platform-schema/build/schema.html#fieldschema

 

To surface those nested fields you might have to map through those recursively and give them “namespaced” keys (e.g. “address~~city”) so you can do the transform in reverse to get them back into the correct structure before sending the data to your API.