When creating event-driven or API flows in IBM App Connect, you typically use the response or output from an event, action, or toolbox node as input in a subsequent action by mapping the fields. In certain cases where data is returned using JavaScript Object Notation (JSON), the data is provided as a string written in JSON format. To map this data in a subsequent action, you must first use App Connect’s built-in JSON parser to convert the data into a JSON object that represents the string.

The schema that the JSON parser generates is formatted as a standard set of comma-separated key/value pairs that are surrounded by curly braces {}. The keys are defined as strings, and their values can be one of the following valid JSON data types: string, number, object, array, boolean, or null. Once generated, these keys will be included in the list of fields that are available for mapping when you click Insert a reference Insert a reference icon in a subsequent action.

Defining the data to be parsed and generating valid JSON schema

Although the JSON parser can be used to parse JSON strings from various applications, a typical use case involves using an HTTP invoke action to make a GET, POST, PUT, PATCH, DELETE, or HEAD request that returns a response from an HTTP endpoint.

The following example depicts an HTTP invoke action within a multi-node flow. Here, a GET call is being made to an HTTP Client Testing Service at http://httpbin.org.

Sample field entries for an HTTP invoke action

To make the response headers and response body available for mapping in a subsequent action, you must insert a JSON parser node before adding the action, and then use the node to define the data that you want to parse, and to generate valid JSON schema. You can insert and configure the node by following these steps:

  1. Click the (+), go to the Toolbox tab, and then click JSON parser.

    Selecting the JSON parser from the Toolbox tab

  2. Use the JSON Input field to specify which data from the previous action or event should be parsed.

    JSON Input field

    Considerations for completing the JSON Input field:

    • You can map to fields that are shown in the list of available inputs (generally accessible by clicking Insert a reference Insert a reference icon). For example:
      JSON Input entry showing a mapped field
    • You can combine text with mapped fields. For example:
      JSON Input entry showing a combination of text and mapped fields
      JSON Input entry showing another combination of text and mapped fields
    • When resolved, the entry in the JSON Input field must produce valid JSON. To prevent runtime errors, any mapped field that you include from a preceding event or action must have a value that transforms into valid input. Based on the data type of the mapped field, you might also need to enclose it in double quotation marks or curly braces {}. For example:
      • If the Response body for a previous HTTP action was returned in JSON format as {"name":"Bob", "age":"99"}, a JSON Input entry of {"responseBody": Response body} will resolve to the following valid JSON:
        {“responseBody”: {“name”:”Bob”, “age”:”99″}}
      • If the Response body for a previous HTTP action was returned in plain text as Blob, a JSON Input entry of {"responseBody": Response body} will produce invalid JSON:
        {“responseBody”: Blob}
        To make the JSON valid, the JSON Input entry must be specified as {"responseBody": "Response body"} – that is, Response body must be enclosed in double quotation marks. The JSON would be considered valid even if no response was returned because the JSON Input entry would resolve to {"responseBody": ""}.
      • If the Request headers (JSON format) field for a previous HTTP action was left blank, a JSON Input entry of Request headers (JSON format) will resolve to an empty value.
      • If the Request headers (JSON format) field for a previous HTTP action was left blank, a JSON Input entry of {"headers": Request headers (JSON format)} will produce invalid JSON:
        {“headers”:}
        To make the JSON valid, the JSON Input entry must be specified as {"headers": {Request headers (JSON format)}} – that is, Request headers (JSON format) must be enclosed in curly braces.

  3. Expand the Output Schema section and use the Example JSON field to specify sample JSON that you expect to be returned (and which should be converted to a JSON object).

    The following example shows sample JSON from a GET call to the HTTP Client Testing Service mentioned earlier (http://httpbin.org/get).

    Example JSON field

    Considerations for completing the Example JSON field:

    • You can copy a sample response returned by a web server or API call for example, and paste it into this field.
    • You can enter a JSON string directly into this field. For example:
      {“name”:”Bob”,”age”:”99″}
    • The Example JSON field accepts a maximum of 16,000 characters. This facility is provided for smaller API responses only because schema generation can be resource intensive and result in performance issues.

      For larger responses, external tools are available which you can use for schema generation if required. Once generated, you can then paste the schema directly into the JSON Schema field (shown in the next step).

  4. To convert the “Example JSON” into a JSON object, click Generate Schema. You can see the result in the JSON Schema field.

    Flow with a JSON Parser node: generated JSON schema

    Tip: You can alternatively bypass the schema generation step and simply enter valid schema directly into the JSON Schema field.

When you add another target application and corresponding action to the flow, you should be able to see the keys in the JSON schema as fields that you can map to.

Example

Event-driven flow with a JSON Parser node

Using the JSON parser to convert the response from an HTTP call for subsequent use in a flow

Learn how to use App Connect Designer to create an event-driven flow that parses the response from an HTTP endpoint so that some of the data can be inserted into an email to recipients.

Join The Discussion

Your email address will not be published. Required fields are marked *