从 Greenplum 中的 JSON 中删除 NULL 字段
Remove NULL fields from JSON in Greenplum
使用基于 Postgres 8.4 的 Greenplum 5.* 数据库。
我正在使用 row_to_json 和 array_to_json 函数创建 JSON 输出;但这最终导致 JSON 中的键具有空值。 Postgres 最新版本有 json_strip_null 函数来删除具有空值的键。
我需要将生成的 JSON 个文件导入到 MongoDB;但是 mongoimport 也没有选择忽略 JSON.
中的空键
我试过的一种方法是用 null 创建 JSON 文件,然后使用 sed 从 JSON 文件中删除 null 字段。
sed -i 's/\(\(,*\)"[a-z_]*[0-9]*":null\(,*\)\)*//g' output.json
但是正在寻找一种方法来实现数据库本身,因为它会更快。关于如何在不影响查询性能的情况下在 Greenplum 中呈现 json_strip_null 函数有什么建议吗?
您可以使用 COALESCE 将空值替换为空字符串或其他值。
https://www.postgresql.org/docs/8.3/functions-conditional.html
COALESCE 函数 returns 它的第一个参数不为空。仅当所有参数都为 null 时才返回 Null。它通常用于在检索数据进行显示时用默认值替换空值,例如:
SELECT COALESCE(描述, short_description, '(none)') ...
这个returns说明如果不为空,否则short_description如果不为空,否则(none)。
...
我在 pg8.3 上的 GP 5.17 中遇到了同样的问题 - 并且已经成功地使用这个正则表达式删除了空值键对。我在 json 列的初始插入中使用它,但是您可以适应:
select
col5,
col6,
regexp_replace(regexp_replace(
(SELECT row_to_json(j) FROM
(SELECT
col1,col2,col3,col4
) AS j)::text,
'(?!{|,)("[^"]+":null[,]*)','','g'),'(,})$','}')::json
AS nvp_json
from foo
从内到外工作,row_to_json 构造函数的结果首先转换为文本,然后内部正则表达式替换任何 "name":null,
值,外部正则表达式修剪任何悬挂的逗号结束,最后整个事情被投回 json.
我用plpython函数解决了这个问题。此通用函数可用于从任何 JSON 中删除 null 和空值键。
CREATE OR REPLACE FUNCTION json_strip_null(json_with_nulls json)
RETURNS text
AS $$
import json
def clean_empty(d):
if not isinstance(d, (dict, list)):
return d
if isinstance(d, list):
return [v for v in (clean_empty(v) for v in d) if v not in (None, '')]
return {k: v for k, v in ((k, clean_empty(v)) for k, v in d.items()) if v not in (None, '')}
json_to_dict = json.loads(json_with_nulls)
json_without_nulls = clean_empty(json_to_dict)
return json.dumps(json_without_nulls, separators=(',', ':'))
$$ LANGUAGE plpythonu;
这个函数可以用作,
SELECT json_strip_null(row_to_json(t))
FROM table t;
使用基于 Postgres 8.4 的 Greenplum 5.* 数据库。
我正在使用 row_to_json 和 array_to_json 函数创建 JSON 输出;但这最终导致 JSON 中的键具有空值。 Postgres 最新版本有 json_strip_null 函数来删除具有空值的键。
我需要将生成的 JSON 个文件导入到 MongoDB;但是 mongoimport 也没有选择忽略 JSON.
中的空键我试过的一种方法是用 null 创建 JSON 文件,然后使用 sed 从 JSON 文件中删除 null 字段。
sed -i 's/\(\(,*\)"[a-z_]*[0-9]*":null\(,*\)\)*//g' output.json
但是正在寻找一种方法来实现数据库本身,因为它会更快。关于如何在不影响查询性能的情况下在 Greenplum 中呈现 json_strip_null 函数有什么建议吗?
您可以使用 COALESCE 将空值替换为空字符串或其他值。
https://www.postgresql.org/docs/8.3/functions-conditional.html
COALESCE 函数 returns 它的第一个参数不为空。仅当所有参数都为 null 时才返回 Null。它通常用于在检索数据进行显示时用默认值替换空值,例如:
SELECT COALESCE(描述, short_description, '(none)') ... 这个returns说明如果不为空,否则short_description如果不为空,否则(none)。 ...
我在 pg8.3 上的 GP 5.17 中遇到了同样的问题 - 并且已经成功地使用这个正则表达式删除了空值键对。我在 json 列的初始插入中使用它,但是您可以适应:
select
col5,
col6,
regexp_replace(regexp_replace(
(SELECT row_to_json(j) FROM
(SELECT
col1,col2,col3,col4
) AS j)::text,
'(?!{|,)("[^"]+":null[,]*)','','g'),'(,})$','}')::json
AS nvp_json
from foo
从内到外工作,row_to_json 构造函数的结果首先转换为文本,然后内部正则表达式替换任何 "name":null,
值,外部正则表达式修剪任何悬挂的逗号结束,最后整个事情被投回 json.
我用plpython函数解决了这个问题。此通用函数可用于从任何 JSON 中删除 null 和空值键。
CREATE OR REPLACE FUNCTION json_strip_null(json_with_nulls json) RETURNS text AS $$ import json def clean_empty(d): if not isinstance(d, (dict, list)): return d if isinstance(d, list): return [v for v in (clean_empty(v) for v in d) if v not in (None, '')] return {k: v for k, v in ((k, clean_empty(v)) for k, v in d.items()) if v not in (None, '')} json_to_dict = json.loads(json_with_nulls) json_without_nulls = clean_empty(json_to_dict) return json.dumps(json_without_nulls, separators=(',', ':')) $$ LANGUAGE plpythonu;
这个函数可以用作,
SELECT json_strip_null(row_to_json(t)) FROM table t;