Creating a graph from a JSON file representing recursive nodes of unknown type

Neo4j 5.8 Enterprise edition

I have a complex json file including some recursive nodes extension. The deep of the structure is unknown when received. The question is: how can I import and create such a structure using cypher?

Is there some way to approach a recursive definition like this?

The idea behind is that any extension has an ury as a string, and a value that can be anything, from a string, to a complex object to another extension.

{
    "resourceType": "Patient",
    "id": "be446404-4982-41d5-bdd3-b1dae0d26bd3",
    "extension": [
      {
        "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity",
        "extension": [
          {
            "url": "detailed",
            "valueCoding": {
              "system": "urn:oid:2.16.840.1.113883.6.238",
              "code": "2186-5",
              "display": "Hispanic or Latino",
              "userSelected": false
            }
          },
          {
            "url": "text",
            "valueString": "Not Hispanic or Latino"
          }
        ]
      },
      {
        "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race",
        "extension": [
          {
            "url": "ombCategory",
            "valueCoding": {
              "system": "urn:oid:2.16.840.1.113883.6.238",
              "code": "2028-9",
              "display": "ASIAN",
              "userSelected": false
            }
          },
          {
            "url": "text",
            "valueString": "Asian"
          }
        ]
      }
    ]
  }

@glilienfield @michael.hunger : Please try to understand if it is possible without writing an extension .... Thank you

@paolodipietro58, welcome back.

I am sorry, but I don't know of a way to implement a recursive algorithm in cypher. It wasn't built that way. I could probably come up with something if the depth was bounded, as it could be written without recursion. It would most likely be complicated as the depth grows.

Are you using a language driver? If so, my suggestion would be to process the json in your application and construct the entity using the driver. You could do the whole thing in one transaction using a transaction function (not called that any more, but executeWrite statement). That way you could error out if necessary and the transaction would be rolled back.

I use java, so I would deserialize the json into a Map<String, Object> using json library (I use Jackson2 because is it available by default in Spring Boot applications). I would then write a method that processes an 'extension'. This same method can be recursively called for each extension's child extension, until no child extension exists. At each stage you create the extension's node and then call the method again passing the child extension. This would all be done within one transaction.

Sorry I could not help further. Cypher is very powerful, but it is not a scripting language.

1 Like

Hi @glilienfield , thank you for your answer.

Asyou can imagine, it's not what I was waiting for. This time we are using C# (.NET) as a language driver. We'll try to doas you suggest, and let you know the results.

I believe it would be fairly easy as I described..Actually, much easier than if a cypher solution was available. I have written many recursive algorithms that traverse graphs doing different things. It is not so bad once you wrap your head around it.

Let me know if I can help further.