This article demonstrates how to handle uncluttered JSON strings and convert them to a structured JSON object format using JSON Parser by Quickwork.
What you'll need:
Skill level: Intermediate
Time required: 5 minutes
- Sample JSON data in the string format
- Skills to write 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 a JSON Parser to get structured JSON output from raw 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 the user because data shown to the user needs to be in the human interpretable form or in a presentable format. However, there may arise situations where you need to deal with data that is raw and unprocessed. In such cases, the JSON parser can be used to parse the data and convert it into pure and structured JSON format:
- An object of raw JSON data
- An array of objects containing raw JSON data
Rules for specifying the raw JSON data:
- It should be enclosed within the box bracket
{...}
. if it is an array of objects. - The last curly bracket
}
of the last JSON object in an array 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 may throw an error as Bad request
.
Passing a string containing JSON object(s)
- Under the Event section, choose the Scheduler by Quickwork app from the drop-down menu in the Apps field present right below the New Trigger button.
- Select the trigger event, New scheduled event, from the drop-down menu in the Triggers field.
- Set the Interval based on your requirement depending on how frequently you want the journey to be executed.
- Set the date and time of your choice in the Start At field and keep the Custom Payload field empty:
- Under the Steps section, click on the Simple Action button and choose the JSON Parser by Quickwork app from the drop-down menu in the Apps field.
- Select Parse string to JSON from the drop-down menu in the Actions field.
- In the String field, you can simply pass any string containing JSON objects or any data pill from the above steps that contains a string containing JSON object(s). For example,
{ "userId": 123, "id": 40, "Name": "Mr. John Snow", "Designation": "Sr. Software Engineer"}.
- In the Sample JSON field, define a syntax for the JSON response you want. For example:
{
"user_id": 1,
"personal_id": 4,
"full_name": "",
"job_profile": ""
}
Note: If a key in your raw JSON data has value as aninteger
, the syntax should have an equivalent key-value pair like"A": 1
which also represents the data type. This means a dummy value must be passed to the key while defining the syntax to indicate the data type the key is supposed to have to the parser. If a key in your raw JSON data has the value of astring
, the syntax should be"A": ""
. This means a null string can be passed to the key while defining the syntax. If this is not followed, further actions may not get the relevant data pills and may eventually throw an error. - Save the changes and start the journey. Go to the History section and check the execution details.
- The Input block of the JSON Parser action contains two keys:
-
document
: The raw JSON data that needs to be parsed. sampleDocument
: The syntax for the JSON response.
-
- The Output block 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 needs to be defined only once:
- Upon successful execution of the journey, the array will be converted to a more presentable JSON format as illustrated here:
Also, the respective keys will cater as data pills in the Data Tree Output as shown above which can be used in further actions when required.
Comments
0 comments
Please sign in to leave a comment.