This article demonstrates how to handle uncluttered JSON string and convert it to a structured JSON format using JSON Parser by Quickwork.
What you'll need:
Skill level: Intermediate
Time required: 5 minutes
- A sample JSON data in the string format
- Skills to specify raw JSON data and syntax for JSON response
- JSON Parser by Quickwork as an action
- Scheduler by Quickwork as a trigger--for triggering the journey
How to use JSON Parser to get the structured response of your raw JSON data
JSON (JavaScript Object Notation) is a syntax for storing data. It is based on name-value pairs and arrays, put together in a simple format which makes it highly readable and easily transmitted.
JSON is mostly hidden from users because we expose data as input fields and output data pills in the journey. However, you may occasionally handle raw JSON data in some cases. You can pass two types of raw JSON data to get parsed into pure JSON format:
- An object of raw JSON data
- An array of objects containing raw JSON data
Rules or specifying the raw JSON data:
- It should be within the box bracket
[...]
. - The last curly bracket
}
of the last JSON object should not have a comma,
. - The key-value pairs of string data should be enclosed within double quotes
""
.
Violation of any of the above rules will throw an error as Bad request
.
Passing an object of raw JSON data
- Under the Trigger section, choose the Scheduler by Quickwork app from the drop-down menu in the App field.
- Select the trigger event, New scheduled event, from the drop-down menu in the Trigger Event field.
- Set the Interval for One day as we need to fetch the breaking news on a daily basis.
- Set the date and time of your choice in the Start At field and keep the Custom Payload field empty:
- Under the Steps section, choose the JSON Parser by Quickwork app from the drop-down menu in the App field.
- Select Parse string to JSON from the drop-down menu in the Action field.
- In the String field, pass an object of raw JSON data that you want to parse to a proper JSON format. For example,
{ "userId": 123, "id": 40, "Name": "Mr. John Snow", "Designation": "Sr. Software Engineer"}.
- In the Sample JSON field, define a syntax for JSON response. For example:
{
"A": 1,
"B": 4,
"C": "",
"D": ""
}
Note: If a key in your raw JSON data has value as aninteger
, the syntax should be"A": 1
. This means a dummy value must be passed to the key while defining a syntax. If a key in your raw JSON data has value as astring
, the syntax should be"A": ""
. This means a null value must be passed to the key while defining a syntax. If this is not followed, further actions will not get the relevant data pills and will eventually throw an error. - Save the changes and start the journey. Go to the History section and check the execution details:
- The Input of the JSON Parser contains two keys:
-
document
: The raw JSON data that needs to be parsed. sampleDocument
: The syntax for the JSON response.
-
- The Output contains the JSON data of the raw string passed:
Passing an array of objects containing raw JSON data
- In the String field of JSON Parser action, specify an array containing objects of raw JSON data. For example:
[
{"userId": 10, "id": 12343, "name": "Mr. Andrew Flintoff", "designation": "Technical writer"},
{"userId": 10, "id": 62743, "name": "Mrs. Stefie Hall", "designation": "Production designer"},
{"userId": 10, "id": 76348, "name": "Mr. Roman Hall", "designation": "Developer"}
] - In the Sample JSON field, define a syntax for JSON response as done previously. Even for an array containing n number of a data object, the syntax is to be defined only once:
- Upon successful execution of the journey, the array of sticky and clumsy-looking raw JSON data object will get molded into a standard JSON format as illustrated here:
Comments
0 comments
Please sign in to leave a comment.