Skip to content

bigfunctions > xml2json

xml2json

Call or Deploy xml2json ?

✅ You can call this xml2json bigfunction directly from your Google Cloud Project (no install required).

  • This xml2json function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want to build and manage your own catalog of functions. This is particularly useful if you want to create private functions (for example calling your internal APIs). Discover the framework

Public BigFunctions Datasets:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

xml2json(xml)

Description

Returns JSON as a string for given XML string

Examples

select bigfunctions.eu.xml2json('<a><b>foo</b></a>')
select bigfunctions.us.xml2json('<a><b>foo</b></a>')
select bigfunctions.europe_west1.xml2json('<a><b>foo</b></a>')
+-------------------+
| json              |
+-------------------+
| {"a":{"b":"foo"}} |
+-------------------+

select bigfunctions.eu.xml2json('<a></a>')
select bigfunctions.us.xml2json('<a></a>')
select bigfunctions.europe_west1.xml2json('<a></a>')
+----------+
| json     |
+----------+
| {"a":""} |
+----------+

select bigfunctions.eu.xml2json('<a></a')
select bigfunctions.us.xml2json('<a></a')
select bigfunctions.europe_west1.xml2json('<a></a')
+------+
| json |
+------+
| null |
+------+

Need help using xml2json?

The community can help! Engage the conversation on Slack

For professional suppport, don't hesitate to chat with us.

Found a bug using xml2json?

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

For professional suppport, don't hesitate to chat with us.

Use cases

Let's say you have a BigQuery table that stores product information, but some of that information is stored in XML format within a string column. You want to analyze this data using BigQuery's powerful SQL capabilities, but working directly with XML in SQL can be cumbersome. The xml2json function provides a solution.

Scenario:

Your table products has columns like product_id, product_name, and product_details. The product_details column contains XML data like this:

<product_details>
  <color>Red</color>
  <size>Large</size>
  <price>25.99</price>
</product_details>

Use Case with xml2json:

You can use xml2json to convert the XML data into JSON within your SQL query, making it easier to access specific elements:

SELECT
    product_id,
    product_name,
    JSON_VALUE(bigfunctions.us.xml2json(product_details), '$.product_details.color') AS color,
    JSON_VALUE(bigfunctions.us.xml2json(product_details), '$.product_details.size') AS size,
    CAST(JSON_VALUE(bigfunctions.us.xml2json(product_details), '$.product_details.price') AS NUMERIC) AS price
FROM
    products;

This query uses xml2json to convert the product_details XML into a JSON string. Then, JSON_VALUE extracts the color, size, and price values using JSONPath expressions. This transforms the XML data into a more manageable format for analysis within BigQuery.

Other Potential Use Cases:

  • Data Transformation for downstream applications: Convert XML data to JSON before exporting it to other systems that work better with JSON.
  • Simplifying complex XML structures: Transform complex, nested XML into a flatter JSON structure for easier querying and reporting.
  • API Integration: If an API returns data in XML format, xml2json can be used to convert the response into JSON within BigQuery for analysis.
  • Log Processing: If log files are stored in XML format, this function can convert them to JSON for easier parsing and analysis within BigQuery.

By converting XML to JSON within BigQuery using xml2json, you unlock the power of BigQuery's JSON functions and make complex XML data more accessible for analysis and processing.

Spread the word

BigFunctions is fully open-source. Help make it a success by spreading the word!

Share on Add a on