August 17, 2023

tExtractJSONFields – Docs for ESB 5.x

tExtractJSONFields

tExtractJSONFields_icon32_white.png

tExtractJSONFields properties

Component family

Processing/Fields

 

Function

tExtractJSONFields extracts the
desired data from incoming JSON fields based on the XPath or
JSONPath query.

Purpose

tExtractJSONFields extracts the
data from JSON fields stored in a file, a database table, etc.,
based on the XPath or JSONPath query.

Basic settings

Property type

Either Built-in or Repository.

 

 

Built-in: No property data is
stored centrally.

 

 

Repository: Select the repository
file in which the properties are stored. The fields that follow are
completed automatically using the data retrieved.

 

Schema and Edit
Schema

A schema is a row description, it defines the number of fields to
be processed and passed on to the next component. The schema is
either Built-in or stored remotely
in the Repository.

Click Edit schema to make changes to the schema. If the
current schema is of the Repository type, three options are
available:

  • View schema: choose this option to view the
    schema only.

  • Change to built-in property: choose this option
    to change the schema to Built-in for local
    changes.

  • Update repository connection: choose this option to change
    the schema stored in the repository and decide whether to propagate the changes to
    all the Jobs upon completion. If you just want to propagate the changes to the
    current Job, you can select No upon completion and
    choose this schema metadata again in the [Repository
    Content]
    window.

 

 

Built-in: The schema will be
created and stored locally for this component only. Related topic:
see Talend Studio User Guide.

 

 

Repository: The schema already
exists and is stored in the Repository, hence can be reused in
various projects and Job designs. Related topic: see
Talend Studio User
Guide
.

 

Read By

Select a way of extracting the specified JSON field from the list,
either Xpath or JsonPath.

 

JSON field

List of the JSON fields to be extracted.

 

Loop XPath query

Node within the JSON field, on which the loop is based.

This field is available only when Xpath is selected from the Read By list.

 

Loop Jsonpath query

Specify the JSONPath node on which the loop is based.

This field is available only when JsonPath is selected from the Read By list.

 

Mapping

Complete this table to map the columns defined in the schema to
the corresponding JSON nodes.

  • Column: The Column cells are automatically
    filled with the defined schema column names.

  • XPath query: Specify the
    XPath node that holds the desired data.

    This column is available only when Xpath is selected from the Read By list.

  • Get Nodes: Select this
    check box to extract the JSON data of all the nodes
    specified in the XPath
    query
    list or select the check box next to a
    specific node to extract the data of that node.

    This column is available only when Xpath is selected from the Read By list.

  • Is Array: Select this
    check box when the JSON field to be extracted is an array
    instead of an object.

    This column is available only when Xpath is selected from the Read By list.

  • Json query: Specify the
    JSONPath node that holds the desired data. For more
    information about JSONPath expressions, see http://goessner.net/articles/JsonPath/.

    This column is available only when JsonPath is selected from the Read By list.

 

Die on error

Select this check box to stop the execution of the Job when an error occurs.

Clear the check box to skip any rows on error and complete the process for error-free rows.
When errors are skipped, you can collect the rows on error using a Row
> Reject
link.

This feature is not available to the Storm
version of this component.

Advanced settings

Encoding

Select the encoding from the list or select Custom and
define it manually. This field is compulsory for database data handling.

tStatCatcher Statistics

Select this check box to gather the Job processing metadata at a
Job level as well as at each component level.

Note that this check box is not available in
both of the Map/Reduce version and the Storm version of the
component.

Global Variables

ERROR_MESSAGE: the error message generated by the
component when an error occurs. This is an After variable and it returns a string. This
variable functions only if the Die on error check box is
cleared, if the component has this check box.

NB_LINE: the number of rows read by an input component or
transferred to an output component. This is an After variable and it returns an
integer.

A Flow variable functions during the execution of a component while an After variable
functions after the execution of the component.

To fill up a field or expression with a variable, press Ctrl +
Space
to access the variable list and choose the variable to use from it.

For further information about variables, see Talend Studio
User Guide.

Usage

This component is an intermediate component. It needs an input and
an output components.

Usage in Map/Reduce Jobs

If you have subscribed to one of the Talend solutions with Big Data, you can also
use this component as a Map/Reduce component. In a Talend Map/Reduce Job, this
component is used as an intermediate step and other components used along with it must be
Map/Reduce components, too. They generate native Map/Reduce code that can be executed
directly in Hadoop.

You need to use the Hadoop Configuration tab in the
Run view to define the connection to a given Hadoop
distribution for the whole Job.

For further information about a Talend Map/Reduce Job, see the sections
describing how to create, convert and configure a Talend Map/Reduce Job of the
Talend Big Data Getting Started Guide.

Note that in this documentation, unless otherwise explicitly stated, a scenario presents
only Standard Jobs, that is to say traditional Talend data
integration Jobs, and non Map/Reduce Jobs.

Usage in Storm Jobs

If you have subscribed to one of the Talend solutions with Big Data, you can also
use this component as a Storm component. In a Talend Storm Job, this component is used as
an intermediate step and other components used along with it must be Storm components, too.
They generate native Storm code that can be executed directly in a Storm system.

The Storm version does not support the use of the global variables.

You need to use the Storm Configuration tab in the
Run view to define the connection to a given Storm
system for the whole Job.

This connection is effective on a per-Job basis.

For further information about a Talend Storm Job, see the sections
describing how to create and configure a Talend Storm Job of the Talend Big Data Getting Started Guide.

Note that in this documentation, unless otherwise explicitly stated, a scenario presents
only Standard Jobs, that is to say traditional Talend data
integration Jobs.

Log4j

The activity of this component can be logged using the log4j feature. For more information on this feature, see Talend Studio User
Guide
.

For more information on the log4j logging levels, see the Apache documentation at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html.

Limitation

Due to license incompatibility, one or more JARs required to use this component are not
provided. You can install the missing JARs for this particular component by clicking the
Install button on the Component tab view. You can also find out and add all missing JARs easily on
the Modules tab in the Integration perspective
of your studio. For details, see https://help.talend.com/display/KB/How+to+install+external+modules+in+the+Talend+products
or the section describing how to configure the Studio in the Talend Installation and Upgrade
Guide
.

Scenario 1: Retrieving error messages while extracting data from JSON fields

In this scenario, tWriteJSONField wraps the incoming
data into JSON fields, data of which is then extracted by tExtractJSONFields. Meanwhile, the error messages generated due to
extraction failure, which include the concerned JSON fields and errors, are retrieved
via a Row > Reject
link.

Linking the components

  1. Drop the following components from the Palette onto the design workspace: tFixedFlowInput, tWriteJSONField, tExtractJSONFields, and tLogRow (X2). The two tLogRow components are renamed as data_extracted and reject_info.

  2. Link tFixedFlowInput and tWriteJSONField using a Row > Main connection.

  3. Link tWriteJSONField and tExtractJSONFields using a Row > Main connection.

  4. Link tExtractJSONFields and data_extracted using a Row > Main connection.

  5. Link tExtractJSONFields and reject_info using a Row > Reject connection.

    use_case_textractjsonfields_1.png

Configuring the components

Setting up the tFixedFlowInput

  1. Double-click tFixedFlowInput to display
    its Basic settings view.

    use_case_textractjsonfields_2.png
  2. Click Edit schema to open the schema
    editor.

    use_case_textractjsonfields_3.png

    Click the [+] button to add three
    columns, namely firstname, lastname and dept, with the type of string.

    Click OK to close the editor.

  3. Select Use Inline Content and enter the
    data below in the Content box:

Setting up the tWriteJSONField

  1. Click tWriteJSONField to display its
    Basic settings view.

    use_case_textractjsonfields_4.png
  2. Click Configure JSON Tree to open the XML
    tree editor.

    use_case_textractjsonfields_5.png

    The schema of tFixedFlowInput appears in
    the Linker source panel.

  3. In the Linker target panel, click the
    default rootTag and type in staff, which is the root node of the JSON field
    to be generated.

  4. Right-click staff and select Add Sub-element from the context menu.

  5. In the pop-up box, enter the sub-node name, namely firstname.

    use_case_textractjsonfields_6.png

    Repeat the steps to add two more sub-nodes, namely lastname and dept.

  6. Right-click firstname and select
    Set As Loop Element from the context
    menu.

  7. Drop firstname from the Linker source panel to its counterpart in the
    Linker target panel.

    In the pop-up dialog box, select Add linker to
    target node
    .

    use_case_textractjsonfields_7.png

    Click OK to close the dialog box.

  8. Repeat the steps to link the two other items.

    Click OK to close the XML tree
    editor.

  9. Click Edit schema to open the schema
    editor.

    use_case_textractjsonfields_8.png
  10. Click the [+] button in the right panel
    to add one column, namely staff, which
    will hold the JSON data generated.

    Click OK to close the editor.

Setting up the tExtractJSONFields

  1. Double-click tExtractJSONFields to
    display its Basic settings view.

    use_case_textractjsonfields_9.png
  2. Click Edit schema to open the schema
    editor.

    use_case_textractjsonfields_10.png
  3. Click the [+] button in the right panel
    to add three columns, namely firstname,
    lastname and dept, which will hold the data of their counterpart nodes in
    the JSON field staff.

    Click OK to close the editor.

  4. In the pop-up Propagate box, click
    Yes to propagate the schema to the
    subsequent components.

    use_case_textractjsonfields_13.png
  5. In the Loop XPath query field, enter
    “/staff”, which is the root node of
    the JSON data.

  6. In the Mapping area, type in the node
    name of the JSON data under the XPath query
    part. The data of those nodes will be extracted and passed to their
    counterpart columns defined in the output schema.

  7. Specifically, define the XPath query “firstname” for the column firstname, “lastname” for
    the column lastname, and “” for the column dept. Note that “” is not
    a valid XPath query and will lead to execution errors.

Setting up the tLogRow components

  1. Double-click data_extracted to display
    its Basic settings view.

    use_case_textractjsonfields_11.png
  2. Select Table (print values in cells of a
    table)
    for a better display of the results.

  3. Perform the same setup on the other tLogRow component, namely reject_info.

Executing the Job

  1. Press Ctrl + S to save the Job.

  2. Click F6 to execute the Job.

    use_case_textractjsonfields_12.png

    As shown above, the reject row offers such details as the data extracted,
    the JSON fields whose data is not extracted and the cause of the extraction
    failure.

Scenario 2: Collecting data from your favorite online social network

In this scenario, tFileInputJSON retrieves the
friends node from a JSON file that contains the
data of a Facebook user and tExtractJSONFields extracts
the data from the friends node for flat data
output.

Linking the components

  1. Drop the following components from the Palette onto the design workspace: tFileInputJSON, tExtractJSONFields and tLogRow.

  2. Link tFileInputJSON and tExtractJSONFields using a Row > Main connection.

  3. Link tExtractJSONFields and tLogRow using a Row > Main connection.

    use_case_textractjsonfields_2_1.png

Configuring the components

  1. Double-click tFileInputJSON to display
    its Basic settings view.

    use_case_textractjsonfields_2_2.png
  2. Click Edit schema to open the schema
    editor.

    use_case_textractjsonfields_2_3.png

    Click the [+] button to add one column,
    namely friends, of the String
    type.

    Click OK to close the editor.

  3. Click the […] button to browse for the
    JSON file, facebook.json in this
    case:

  4. Clear the Read by XPath check box.

    In the Mapping table, enter the JSONPath
    query “$.user.friends[*]” next to the
    friends column, retrieving the entire
    friends node from the source
    file.

  5. Double-click tExtractJSONFields to
    display its Basic settings view.

    use_case_textractjsonfields_2_4.png
  6. Click Edit schema to open the schema
    editor.

    use_case_textractjsonfields_2_5.png
  7. Click the [+] button in the right panel
    to add five columns, namely id, name, like_id, like_name and
    like_category, which will hold the
    data of relevant nodes in the JSON field friends.

    Click OK to close the editor.

  8. In the pop-up Propagate box, click
    Yes to propagate the schema to the
    subsequent components.

    use_case_textractjsonfields_13.png
  9. In the Loop XPath query field, enter
    “/likes/data”.

  10. In the Mapping area, type in the queries
    of the JSON nodes in the XPath query
    column. The data of those nodes will be extracted and passed to their
    counterpart columns defined in the output schema.

  11. Specifically, define the XPath query “../../id” (querying the “/friends/id” node) for the column id, “../../name”
    (querying the “/friends/name” node) for
    the column name, “id” for the column like_id, “name” for the
    column like_name, and “category” for the column like_category.

  12. Double-click tLogRow to display its
    Basic settings view.

    use_case_textractjsonfields_11.png
  13. Select Table (print values in cells of a
    table)
    for a better display of the results.

Executing the Job

  1. Press Ctrl + S to save the Job.

  2. Click F6 to execute the Job.

    use_case_textractjsonfields_2_6.png

    As shown above, the friends data of the Facebook user Kelly Clarkson is
    extracted correctly.


Document get from Talend https://help.talend.com
Thank you for watching.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x