Learn how to create and parse JSON within a scenario to support your design needs.
Workfront recommends watching the exercise walkthrough video before trying to recreate the exercise in your own environment.
In this video, you will learn how to:
The purpose of the next walkthrough exercise is to conceptually show you how to utilize information sent into your scenario in a JSON format, parsing it into fields and items that you can map throughout your scenario and then either grabbing information from those mapped arrays or aggregating the information into JSON to then be sent out to another system that expects JSON as a receiving input. To be successful at this walkthrough exercise, you will want to create a new scenario which you can title Working with JSON Donut Data, and within the walkthrough exercise folder, you’ll want to utilize the Donut Data - Sample JSON Word document. This isn’t a file that you need to make a copy of for your own personal use but you can simply get into the file and copy the information within, which we’ll talk about and examine in the first exercise video.
Let’s start by taking a closer look at the sample JSON file titled Donut Data in the walkthrough exercise folder. If I zoom in on this information and recall what we learned in the previous course on working with arrays, you’ll remember that in JSON, an array is identified by the square or box brackets, and a collection is identified by the curly brackets. And finally, each key value pair is going to be identified by a key in quotes separated by a colon and then the value right after. So we can see some simple key value pairs at the beginning of our collection, within our array. And then down below, we have another collection within the collection containing an array of key value pairs: an ID, an ID number, a type, and a value. If I zoom out, I can see that we have one collection here, a second collection here, and on the second page, a third collection within the array. Each collection contains different variable numbers of batter types and topping types. I’m going to click anywhere in this information select Command or Control + A to select all and then Command or Control + C to copy the information which we’ll use in the next video as we begin building our scenario.
Now that we’ve copied this JSON, let’s go into our Fusion System and create a new scenario titled Working with JSON Donut Data. For our trigger module, we’re going to use the set variable module.
For the variable name, we’ll simply say Donut Data. For the variable lifetime, we’ll leave it as one cycle. And for the value, we’ll do a Command or Control + V to paste in all of the JSON we copied. Go ahead and click OK. If you’d like, you can rename this module JSON from another connector.
Now that we’re sending JSON data through our scenario, we’ll want to parse it so that we can use the variable information throughout the rest of the scenario. So I’ll click Add another module and I’ll choose the Parse JSON module.
Then we need to create a data structure for the JSON coming in. Since we already have it copied on our clipboard, we can simply click Add, select the Generator, and paste in that sample data again. Once you click Save, make sure you give your sample data a name such as Donut Data.
Now, our JSON string that we want to pass through our data structure is going to be the set variable from the previous module.
Let’s go ahead and save and run once to see the type of information that will pass through our scenario.
Clicking into the execution inspector on our trigger module we can see that the bundle output data is just a long JSON string. If I go into the execution inspector for our Parse JSON, I can see that we now have a structured list of three bundles of information containing collections and arrays for the batters and the toppings as we expected from examining our JSON Sample Data.
Now that we parse the JSON into items and values that we can map and use throughout our scenario, let’s perform a couple different actions with those items. I’ll start by adding a router after our Parse JSON module.
Once I do that in my first path, I’ll use a set variable module.
For the variable name, I’ll simply type Batter Types By Donut.
And for the value, I know that the batter types are contained within an array so I’m going to go to my array functions and choose the mapping option which we’ve used in other walkthrough exercises. In the first space in the mapping function, I want to put in the array that I’m going to dig into and map to a specific item. After the semicolon, I want to open the batter array and make sure that I use the exact same name as the item from that module type.
This will give me all types of batter per donut or bundle passed into this set variable module. If I click okay and run the scenario one time, we’ll see that three bundles of information were passed into this last module, and that if I look into each one, I can see that the output for the first donut type came as regular, chocolate, blueberry, and devil’s food batter types. The output for the second bundle of information or donut pass through is regular, and the batter types for the third bundle is regular and chocolate. If I only want to look at or use one specific type of batter throughout the rest of my scenario, then I can map to the specific item in the list. To do that, I’ll go back into my set variable module and after mapping to a specific field within the array, I can add another semicolon and say that I want to go in and for each type I only care about pulling information on the chocolate type.
If I click OK and run once again, we can open the execution inspector and see that yet again three bundles were passed through, but the output on the first is just the chocolate batter type. The output on the second is nothing because it did not contain a chocolate batter type. And the output on the third also had chocolate in it.
So the lesson to be learned here is that we can map to a specific field within an array which will result in multiple outputs, but then we can also filter down to a specific item in that array by using the field name or the key name and the value. If we go back to our JSON, we can see that here we have an ID and an ID number as a key value pair, but we also have a type and the name of the batter type as a key value pair. So I can either filter by the ID or I can filter by the type to pull out that specific piece of information from the array.
Now, let’s say that after you’ve parsed the JSON, you’ve manipulated it or changed it in your scenario for whatever uses you had, but then you need to pass it to another system as JSON yet again, what you can do is choose from one of three JSON modules.
If I click into my second path in the JSON app, I can see that there’s a Transform to JSON module, a Create JSON module, and an Aggregate to JSON module. We won’t show all three, but we will choose the Aggregate to JSON since we have three bundles passing through after our Parse JSON module. I click aggregate to JSON. I do need to choose like similar aggregators. Which module is my iterator? In this case, it’s our Parse JSON. And then I do need to define the data structure for the JSON that I want to send out. Of course, you could redefine the data structure and map accordingly, but in this case, I’m going to simply choose our Donut Data data structure from our previous video, and then I have to map each of the values from our Parse JSON to create our new JSON that we’re going to send out. In this case, I’ll simply map everything over just as is but you could of course use the mapping panel in a different data structure to completely change how you’re passing information out of your scenario.
What you’ll notice after mapping over the ID, type, name, and PPU fields from our Parse JSON module that batters and toppings appear a little bit different. They’re not simply a field to map values into. This is because within these fields, batters was actually a collection, and batter is an array. So we actually have to click Add an item and then add the batter ID which is an item or variable in that array, and type as well.
We’ll do the same for topping.
Once we do all of that, we can click OK and run the scenario one time.
Once it runs, we can see by the number on the execution inspector that in the top path we ran through and executed on that module three times. But below, since we’re aggregating all three bundles into one, we only have one operation performed. If I click into that execution inspector I can see the three bundles of information which were all mapped fields from our Parse JSON module. They’ve been turned into a JSON string down below which you could then send off to another third party platform for digestion or use.
Now, of course, this was a conceptual walkthrough exercise of how to parse JSON and use JSON, but hopefully you can see how you can use this universal language between systems to create data structures that you need to use throughout your scenario but then you can send that information on in a usable way by other systems. -
For step-by-step instructions on completing the walkthrough, go to the Working with JSON walkthrough exercise.