apoc.convert.fromJsonList
语法 |
|
||
描述 |
将给定的 JSON 列表转换为 Cypher |
||
参数 |
名称 |
类型 |
描述 |
|
|
一个 JSON 字符串化的列表。 |
|
|
|
用于从列表中提取特定部分的 JSON 路径表达式。默认值为空字符串 ``。 |
|
|
|
JSON 路径选项:('ALWAYS_RETURN_LIST', 'AS_PATH_LIST', 'DEFAULT_PATH_LEAF_TO_NULL', 'REQUIRE_PROPERTIES', 'SUPPRESS_EXCEPTIONS') 默认值为: |
|
返回 |
|
||
使用示例
以下将 JSON 列表转换为 Cypher 列表
RETURN apoc.convert.fromJsonList('[1,2,3]') AS output;
| 输出 |
|---|
[1, 2, 3] |
我们还可以使用 JSON 路径表达式来提取 JSON 列表的一部分。例如,以下从一个 JSON 对象列表中提取 name 属性并返回一个 Cypher 字符串列表
RETURN apoc.convert.fromJsonList('[
{"name": "Neo4j"},
{"name": "Graph Data Science Library"},
{"name": "Bloom"}
]', '.name') AS output;
| 输出 |
|---|
["Neo4j", "Graph Data Science Library", "Bloom"] |
此外,我们可以自定义 JSON 路径选项,将字符串列表作为第三个参数(pathOptions)添加,其中字符串基于 Enum<Option>。默认值为 ["SUPPRESS_EXCEPTIONS", "DEFAULT_PATH_LEAF_TO_NULL"]。请注意,我们也可以插入 [],即“无选项”。因此我们可以执行(使用默认 pathOptions)
RETURN apoc.convert.fromJsonList('{ "columns": {
"col2": {
"_id": "772col2"
}
}
}', '$..columns') AS output;
| 输出 |
|---|
[ {"col2": { "_id": "772col2" }}, null, null ] |
或者,使用自定义路径选项
RETURN apoc.convert.fromJsonList('{ "columns": {
"col2": {
"_id": "772col2"
}
}
}', '$..columns', ['ALWAYS_RETURN_LIST']) AS output;
| 输出 |
|---|
[ {"col2": { "_id": "772col2" }} ] |
如果我们尝试转换非列表结构,将会得到一个异常。例如
RETURN apoc.convert.fromJsonList('{"name": "Neo4j"}') AS output;
调用函数 |
在这种情况下,我们应该改用 apoc.convert.fromJsonMap。