JSON_EACH

Extracts key-value pairs from a JSON object, breaking down the structure into individual rows in the result set. Each row represents a distinct key-value pair derived from the input JSON expression.

Analyze Syntax

func.json_each(<json_string>)

Analyze Example


┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 func.json_each(func.parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}')) 
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 ('age','25')                                                                                               
 ('grades','[90,85,92]')                                                                                    
 ('isStudent','false')                                                                                      
 ('name','"John"')                                                                                          
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

JSON_EACH(<json_string>)

Return Type

JSON_EACH returns a set of tuples, each consisting of a STRING key and a corresponding VARIANT value.

SQL Examples

-- Extract key-value pairs from a JSON object representing information about a person
SELECT
  JSON_EACH(
    PARSE_JSON (
      '{"name": "John", "age": 25, "isStudent": false, "grades": [90, 85, 92]}'
    )
  );


┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
 json_each(parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}')) 
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
 ('age','25')                                                                                     
 ('grades','[90,85,92]')                                                                          
 ('isStudent','false')                                                                            
 ('name','"John"')                                                                                
└──────────────────────────────────────────────────────────────────────────────────────────────────┘

-- Display data types of the extracted values
SELECT
  TYPEOF (
    JSON_EACH(
      PARSE_JSON (
        '{"name": "John", "age": 25, "isStudent": false, "grades": [90, 85, 92]}'
      )
    )
  );

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 typeof(json_each(parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}'))) 
├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 TUPLE(STRING, VARIANT) NULL                                                                              
 TUPLE(STRING, VARIANT) NULL                                                                              
 TUPLE(STRING, VARIANT) NULL                                                                              
 TUPLE(STRING, VARIANT) NULL                                                                              
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Last modified June 12, 2024 at 4:51 PM EST: adding JSON method updates (a7607f7)