Question

Converting JSON into individual items

  • 10 January 2023
  • 8 replies
  • 1608 views

Userlevel 1

Hi All,

I have some JSON that I’m trying to convert into individual items so that I can use the parts in different ways later down the line in Zapier.

 

The data is formatted as follows: 

{
"Title": "Content",
"Subtitle": "Content2",
"Name": "John Smith",
"Date": 1999,
"Description": "Long form text area",
"Summary": "Long form text area",
"Percentage": 80%
}

How can I convert each item into an individual component that I can use in other ways? Ideally to also split the heading from the content (i.e. to also split “Title” from the “Content”. 

 

Any help is appreciated.

 

Thanks.


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

8 replies

Userlevel 7
Badge +14

Hi @rayvox 

Good question.

Check out the related Code Mode topics for guidance: https://community.zapier.com/search?q=Code+Mode&search_type=tag

 

Can you describe your use case more for us to have context?

Where is the data coming from?

How are you trying to use the data?

Userlevel 1

@Troy Tessalone thanks. I’ll have a read of those posts.

I have data coming in as JSON from a separate step in my Zap, formatted exactly as above. I’m trying to split this up so I can enter the data into a relevant column in a spreadsheet. For example, “content2” will be entered into the “subtitle” column in the spreadsheet, whereas “1992” will be entered into the “date” column. So ideally I can split all the headings from the content and have them as items that I can assign to various columns in the spreadsheet. 

I just don’t quite know how to implement it myself. 

Userlevel 7
Badge +14

@rayvox 

Can you post screenshots with how your Zap steps are configured in order for us to have more context?

Userlevel 1

@Troy Tessalone the setup is literally as follows:

JSON datasource > Code by Zapier > Airtable

The JSON data is formatted identically to the example I provided above. The Airtable step just adds a new record and inputs the relevant item into the corresponding column on the base. 

I just don’t know how to configure the Code by Zapier step to transform the above so that I can use each of the components in the Airtable step.

Userlevel 7
Badge +14

@rayvox 

Give this JavaScript Code a try.

 

Code

let Set = JSON.parse(inputData.Set.replace(/%/g,""));

Set["Percentage"] = Set["Percentage"] + "%"; // remove if you want just the number without the %

output = [{Set}];

 

Output

 

Variables to Map from Code Step

 

Userlevel 1

@Troy Tessalone thanks for this.

I’m now getting the following error:

Any ideas on what might be causing this?

Userlevel 7
Badge +14

@rayvox 

Would need to see screenshots of how the Zap step is configured.

Userlevel 1

I’ve just managed to resolve this. For some reason the JSON was prefixing with an erroneous character. I’ve resolved that now and your above code works. Thanks so much for the help.