|| apoc.convert.fromJsonList - APOC 核心文档 - Neo4j 文档

apoc.convert.fromJsonList

详情

语法

apoc.convert.fromJsonList(list [, path, pathOptions ])

描述

将给定的 JSON 列表转换为 Cypher LIST<STRING>

参数

名称

类型

描述

list

STRING

一个 JSON 字符串化的列表。

path

STRING

用于从列表中提取特定部分的 JSON 路径表达式。默认值为空字符串 ``。

pathOptions

LIST<STRING>

JSON 路径选项:('ALWAYS_RETURN_LIST', 'AS_PATH_LIST', 'DEFAULT_PATH_LEAF_TO_NULL', 'REQUIRE_PROPERTIES', 'SUPPRESS_EXCEPTIONS') 默认值为:null

返回

LIST<ANY>

使用示例

以下将 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.fromJsonList 失败:原因:com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法从 START_OBJECT 令牌反序列化 java.util.ArrayList<java.lang.Object> 实例,位置为 [Source: (String)"{"name": "Neo4j"}"; line: 1, column: 1]

在这种情况下,我们应该改用 apoc.convert.fromJsonMap

© . This site is unofficial and not affiliated with Neo4j, Inc.