JSON Kit

This kit contains components that facilitate wrangling JSON objects. It contains the following components: jsonata, validateJson, and xmlToJson.

The jsonata component

Use this component to evaluate a JSONata expressions in your board. JSONata is a versatile JSON query language (a kind of "SQL for JSON"). The component takes a JSON object, applies the JSONata expression to it, and returns the resulting object. See https://jsonata.org/ for more details on the JSONata expression language.

Input ports

jsonata component input ports

The jsonata component has the following input ports:

Output ports

Example

If we send the set the Expression to:

`$join(snippet, '\n')`

and then send the following JSON to the JSON port:

[
  {
    "snippet": "Question: How old is planet Earth?"
  },
  {
    "snippet": "Thought: I wonder how old planet Earth is?"
  }
]

We will get this output from the Result port:

Question: How old is planet Earth?
Thought: I wonder how old planet Earth is?"

Tip

This example is captured in the board above.

Implementation

validateJson

Evaluates whether a string contains valid JSON that (optionally) conforms to a given JSON Schema.

The most common usage of this component is processing LLM output that contains JSON.

It takes in a string input, and first attempts to parse it as JSON. Because the LLM output commonly surrounds JSON with Markdown JSON code block, the component will look for the first code block like that and only look inside of it. If a code block is not found, it will try to parse the entire output as JSON.

If the string successfully parses into JSON, it will attempt to validate this JSON against a provided schema. If the schema is not supplied, it will declare success.

In any other case, the component will throw an error. As with any Breadboard component, this error can be captured by wiring the $error output port (you will need to temporarily turn off "Hide Advanced Ports on components" in Visual Editor Settings to see the port).

Input ports

validateJson component input ports

The validateJson component has the following input ports:

Output ports

Example

If we set the Schema to:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "snippet": {
        "type": "string"
      }
    }
  }
}

And send the following to JSON String input port:

Here's the output:

```json
[
  { "snippet": "Question: How old is planet Earth?" },
  { "snippet": "Thought: I wonder how old planet Earth is?" }
]
```

We will see the following JSON object on the JSON output port:

[
  { "snippet": "Question: How old is planet Earth?" },
  { "snippet": "Thought: I wonder how old planet Earth is?" }
]

Implementation

The xmlToJson component

Use this component to convert XML to JSON. JSON is a sort of lingua franca in Breadboard, so this component is useful when starting with XML data.

This component takes a string as its single input port. It then tries to parse it as XML. If successful, it then converts it to the alt-json format that is described in here.

Input ports

The xmlToJson component has a single input port:

Output Ports

The xmlToJson component has a single output port:

Example

If we send the following string to XML String input port:

<snippets>
  <snippet title="Snippet 1">Question: How old is planet Earth?</snippet>
  <snippet title="Snippet 2">Thought: I wonder how old planet Earth is?</snippet>
</snippets>

We will get this value from JSON output port:

[
  "$doc",
  {
    "snippets": {
      "$t": ["  ", "  "],
      "snippet": [
        {
          "$t": ["Question: How old is planet Earth?"],
          "title": "Snippet 1"
        },
        {
          "$t": ["Thought: I wonder how old planet Earth is?"],
          "title": "Snippet 2"
        }
      ]
    }
  }
]

Implementation: