首页 > 解决方案 > 如何在 neo4j 中加载嵌套的 JSON

问题描述

我有一个 JSON 具有遵循模式的嵌套属性。这是一个简单的示例,其中显示了“envolves_to”如何嵌套和嵌套

{
    species: { 
        name: 'sample name',
        types: [
            {name, id, and another static data}
        ]
    },
    evolves_to: [
        {
            species: [...],
            evolves_to: [...another childs]
        },
        {
            species: [...],
            evolves_to: []           // this is empty, so this branch ends here
        },
        ...anthoer siblings
    ]
}

这是我尝试使用的代码,但最终出现重复代码:

// get parent and childs
unwind value.evo_chain as evo_chain
with evo_chain.species as parent, evo_chain.evolves_to as childs


// create parent and types
create (p:Pokemon {name: parent.name})

with parent, p, childs
unwind parent.types as types
create (p)<-[:TYPE_OF]-(:Pokemon {
    id: types.id,
    name: types.name,
    height: types.height,
    weight: types.weight
})


// childs new parents
with childs as contents, p as superParent
with distinct contents, superParent

// repeat pattern
unwind contents as evo_chain
with evo_chain.species as parent, evo_chain.evolves_to as childs, superParent

create (superParent)<-[:CHILD_OF]-(p:Pokemon {name: parent.name})

with parent, p
unwind parent.types as types
create (p)<-[:TYPE_OF]-(:Pokemon {
    id: types.id,
    name: types.name,
    height: types.height,
    weight: types.weight
})

如何递归地重复搜索并开始创建关系和节点?

标签: jsonneo4j

解决方案


推荐阅读