convert_json_to_struct - BigQuery

Function

convert_json_to_struct(string)

Description

Convert Json To Struct - Converts a JSON <string> to a STRUCT data type.

Example Query


SELECT `justfunctions.eu.convert_json_to_struct`("{ "time": { "new_value": "17:00","old_value": "16:30" }, "price": { "new_value": "1","old_value": "2" } }")
                                            
/*--Output--
- time: {"new_value":"17:00","old_value":"16:30"} - price: {"new_value":"1","old_value":"2"}
*/

Statement

CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.convert_json_to_struct`(`string` string) RETURNS ARRAY<STRUCT<key STRING, value STRING>>
	LANGUAGE js AS r'''try {
  var obj = JSON.parse(string);
  var keys = Object.keys(obj);
  var arr = [];
  for (i = 0; i < keys.length; i++) {
      arr.push({'key': keys[i], 'value': JSON.stringify(obj[keys[i]])});
  }
  return arr;
} catch(e) {
    return '{}';
}
''' OPTIONS ( description = '''Converts a JSON <string> to a STRUCT data type.'''  )

Regions

justfunctions.eu.convert_json_to_struct(string),
justfunctions.us.convert_json_to_struct(string)

Type

JavaScript User Defined Function (JavaScript UDF)

How to Use

Frequently Asked Questions

User-Defined Functions (UDFs) in Google BigQuery are custom functions that you can create to perform operations that aren't available through the standard SQL functions. These UDFs allow you to extend BigQuery's SQL capabilities to suit your specific data processing needs. JustFunctions is a collection of open-source user-defined functions (UDFs).

JustFunctions is a collection of open-source User-Defined Functions (UDFs) designed to extend the capabilities of Google BigQuery. These functions cover a wide range of applications, including text manipulation, URL processing, date processing, email handling, similarity measures, and more. Moreover, JustFunctions is frequently updated to include more use cases.

We welcome any feedback or questions you may have. You can Contact us or report an issue on Github.

Functions and procedures from JustFunctions can be used directly in any of your projects.
To start, simply click 👆 on any function,  Copy  the 'Example Query' and run it in your BigQuery console.
You can also  Copy  the 'Statement' to create your own private user-defined function.

Yes, JustFunctions is completely free to use.

Yes, currently JustFunctions is only available for Google BigQuery. In the future, we will also support PostgreSQL.

See something wrong? Contact us or report an issue on Github.