PostgreSQL – JSON_EXTRACT_PATH_TEXT Function

The json_extract_path_text function is used to extract a specific value from a JSON object or array by specifying a path to the desired element. This function returns the value as text. The basic syntax of the json_extract_path_text function is as shown below:

json_extract_path_text(json_object_or_array, path_element1, path_element2, ..., path_elementN)

Here’s an example of using the json_extract_path_text function:

SELECT json_extract_path_text('{"person": {"name": "Alice", "age": 28}}', 'person', 'name');

This query would return the text value "Alice" by specifying the path person -> name.

Below is the query to return age:

Please note that the path elements you provide correspond to the hierarchy of keys within the JSON object or array. If a key is not present at a specific level, the function will return NULL as shown below:

Similar Posts