JSON Parser in Salesforce Flow

Munawirrahman
4 min readDec 5, 2020

Working with Salesforce, you may have to connect to a lot of integrations with other external services. And when we’re talking about external services, the most common protocol is REST API and when we’re talking about REST API, commonly there will be JSON data that you need to send or you need to parse.

Now, I won’t be talking about how to send JSON data to other external services, I want to talk about how to parse JSON data inside Salesforce Flow, turn the parsed JSON data to variable inside Salesforce flow, and you will be able to process the automation whatever you’d like inside Salesforce Flow.

You may already see this article from unofficialsf or here where basically you can use JSON2Apex to create strongly typed JSON Parser everytime you want to do integration with JSON data. Personally, I don’t like that method because:
1. Sometimes my JSON data can be so complex, that if I put my JSON to JSON2Apex, they’ll return explicit parse code which will be complicated for me to return the data to Flow as variable
2. I should add new piece of apex codes every time I want to parse new JSON data or integrate with new Rest APIs

Coming from that problem, I’m thinking about how to make the JSON Parsing inside flow can be more simple (or generic), so I won’t need to add new apex class or even go to JSON2Apex every time I want to Parse new JSON. This is the solution I came up with

a. I create a new Invocable Method Class that can Parse JSON to variable in Salesforce flow. You can install it here (don’t forget to change login to test.salesforce if you want to install in sandbox):
v.1.1 https://login.salesforce.com/packaging/installPackage.apexp?p0=04t2w000009F5Go
v.1.0 https://login.salesforce.com/packaging/installPackage.apexp?p0=04t2w000009F5Gj
source: https://github.com/munawirrahman/MultipleJSONParser

b. For this article, I want to parse this JSON data

{“id”: “0001”,”total”:200, “repeat”:true,”ppu”: 0.55,”batters”:{“batter”:[{ “id”: “1001”, “type”: “Regular” },{ “id”: “1002”, “type”: “Chocolate” },{ “id”: “1003”, “type”: “Blueberry” },{ “id”: “1004”, “type”: “Devils Food” }]},”topping”:[{ “id”: “5001”, “type”: null},{ “id”: “5002”, “type”: “Glazed” },{ “id”: “5005”, “type”: “Sugar” },{ “id”: “5007”, “type”: “Powdered Sugar” },{ “id”: “5006”, “type”: “Chocolate with Sprinkles” },{ “id”: “5003”, “type”: “Chocolate” },{ “id”: “5004”, “type”: “Maple” }]}

c. For testing purpose I create new Text Template Variable in my flow which basically I copy paste the JSON on point b

d. Create a new invocable action that you just installed and configure the input

There will be 2 mandatory Input fields that you need to fill

  1. Ignore Error? (Boolean) : set to False if you want the flow to break if there’s an error while you do parsing. Set to True if you want the flow to continue if there’s an error while you do parsing and output value will be “[Error Catched]:<The error exception>”
  2. JSON String to Parse: the JSON text you want to parse, in this case variable Test_JSON that I created earlier

After defining 2 mandatory fields you’ll need to define the JSON keys (or field) that you want the value from. In this case, I want to get values from keys
id, total, repeat, ppu, id from batter index 0 from batters, type from batter index 0 from batters, list from topping, and test the ignore error functionality

Here’s the table to get those data (column Field for flow input)

So what I need to do to parse those data is fill the field in Flow Input to something like this:

e. After I run the flow and map the result to screen flow, here is the result I got

There you go, now you won’t need to add new apex codes to map JSON data in Salesforce Flow :)

Limitation to consider:
1. The reason I make this InvocableMethod up to 20 fields is because I don’t want people who use this hit the iteration limits when doing JSON Parsing so you only need one apex action element to parse the JSON
2. This InvocableMethod only supports 20 Fields in one Apex Action Element, feel free to add the numbers if you need to :)
3. This InvocableMethod returns variable as text variable, don’t forget to convert the text to other data type in flow if you need to

*P.S: Some of you may notice that parsing the JSON list will be pain in the ass. In the next article, I’ll give you the JSON list converter to Collection Variable, so you’ll be able to loop the JSON inside Salesforce flow :)
*edited: you can see the article here.

--

--