Skip to main content Skip to complementary content

Validating raw input JSON data using a JSON schema

You can validate JSON columns using the tJSONValidator component.

This scenario applies only to Talend Data Integration, Talend Data Management Platform, Talend Big Data Platform, Talend Real-Time Big Data Platform, Talend MDM Platform, Talend Data Services Platform, Talend MDM Platform and Talend Data Fabric.

Information noteNote: The tJSONValidator component is available only if you have installed the R2024-11 Talend Studio Monthly update or a later one delivered by Talend. For more information, check with your administrator.

About this task

Using raw input data lets you validate each record separately. Each record is validated against the schema, and it is sent through the FLOW (valid) or Reject link (invalid).

If you want to validate all records and consider them all valid or invalid, use a JSON file. For more information, see Validating JSON schema using a JSON file.

This scenario describes a Job which uses:
  • The tFixedFlowInput component to use raw data input.
  • The tJSONValidator component to validate JSON columns.
  • The tLogRow component to output the results.
A Job using the tFixedFlowInput, tJSONValidator, and two tLogRow components.

Setting up the Job

Procedure

  1. Drop the following components from the Palette onto the design workspace: tFixedFlowInput, tJSONValidator and two tLogRow.
  2. Connect tFixedFlowInput to tJSONValidator using a Main link.
  3. Connect tJSONValidator to one tLogRow using the FLOW link.
  4. Connect tJSONValidator to the other tLogRow using the Reject link.

Configuring the input component

Procedure

  1. Double-click tFixedFlowInput to open its Basic settings view in the Component tab.
  2. Click Edit Schema.
  3. In the open dialog box, click the [+] button and add the columns that hold the initial input data.
    In this example, add two columns:
    • id, and select Integer as Type.
    • json, and select String as Type.
  4. Click OK to close the dialog box.
  5. Select Use Inline Content.
  6. Enter your input data in Content.
    In this example:
    1;{"customers":[{"lastName":"Smith","firstName":"Jane","address":{"street":"4876 Felosa Drive","postCode":90017,"city":"Los Angeles","state":"CA","country":"United States"}}]}
    2;{"customers":[{"lastName":"Doe","firstName":"John","address":{"street":"42373 Tenmile","postCode":92203,"city":"Bermuda Dunes","state":"CA","country":"United States"}}]}
    3;{"customers":[{"lastName":"Frye","firstName":"Lucy","address":{"street":"4862 Parkway Street","postCode":"92298","city":"Bermuda Dunes","state":"CA","country":"United States"}}]}
    4;{"customers":[{"lastName":"Joney","firstName":"Seb","address":{"street":"386 Hart Country Lane","postCode":30830,"city":"Waynesboro","state":8,"country":"United States"}}]}
    5;{"customers":[{"lastName":"Ming","firstName":"Li","address":{"street":"321 Da Bei Yao","postCode":10012,"city":"Bei Jing","state":"","country":"China"}}]}

Configuring the tJSONValidator component

Procedure

  1. Double-click tJSONValidator to display the Basic settings view and define the component properties.
  2. If required, click Sync columns to retrieve the schema defined in the input component.
  3. Click Add to add rows to the Allocate area.
  4. Select the input column from the drop-down list.
  5. Define the schema against which the data must be validated by entering the path to the schema file in double quotes.
    In this example, "inputSchema.json".
    Configuration of tJSONValidator
    Information noteNote: When the $schema dialect is not defined in the schema, define the JSON schema draft version in the Advanced settings.

Configuring the output component and executing the Job

Procedure

  1. Double-click the tLogRow components to display the Basic settings view.
  2. Select Table (print values in cells of a table) for both components.
  3. Press F6 to save the Job and execute it.

Results

Each valid record is sent through the FLOW link, and each invalid record is sent through the Reject link.
.--+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
|                                                                                   tLogRow_3(OK)                                                                                  |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|id|jsonValue                                                                                                                                                                    |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|1 |{"customers":[{"lastName":"Smith","firstName":"Jane","address":{"street":"4876 Felosa Drive","postCode":90017,"city":"Los Angeles","state":"CA","country":"United States"}}]}|
|2 |{"customers":[{"lastName":"Doe","firstName":"John","address":{"street":"42373 Tenmile","postCode":92203,"city":"Bermuda Dunes","state":"CA","country":"United States"}}]}    |
|5 |{"customers":[{"lastName":"Ming","firstName":"Li","address":{"street":"321 Da Bei Yao","postCode":10012,"city":"Bei Jing","state":"","country":"China"}}]}                   |
'--+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------'

.--+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
|                                                                                                                                                                                                                                                                                                                     tLogRow_4(NOK)                                                                                                                                                                                                                                                                                                                      |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|id|jsonValue                                                                                                                                                                         |ExecutionResult                                                                                                                                                                                                                                                                                                                                                                                                                                               |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|3 |{"customers":[{"lastName":"Frye","firstName":"Lucy","address":{"street":"4862 Parkway Street","postCode":"92298","city":"Bermuda Dunes","state":"CA","country":"United States"}}]}|{"ExecutionResult":{"Status":"Invalid","Validations":[{"ColumnName":"jsonValue","Violations":[{"SchemaLocation":"https://example.com/arrays.schema.json#/$defs/customer/properties/address/properties/postCode/type","ErrorCode":"1029","ErrorType":"type","ErrorMessage":"$.customers[0].address.postCode: string found, integer expected","ElementValue":"92298","ElementLocation":"$.customers[0].address.postCode"}],"SchemaURI":"C:/inputSchema.json"}]}}|
|4 |{"customers":[{"lastName":"Joney","firstName":"Seb","address":{"street":"386 Hart Country Lane","postCode":30830,"city":"Waynesboro","state":8,"country":"United States"}}]}      |{"ExecutionResult":{"Status":"Invalid","Validations":[{"ColumnName":"jsonValue","Violations":[{"SchemaLocation":"https://example.com/arrays.schema.json#/$defs/customer/properties/address/properties/state/type","ErrorCode":"1029","ErrorType":"type","ErrorMessage":"$.customers[0].address.state: integer found, string expected","ElementLocation":"$.customers[0].address.state"}],"SchemaURI":"C:/inputSchema.json"}]}}                                |
'--+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!