Managing Inputs with same name but different type

Hi,
I have a client that fetches JSON from a webservice and then stores in in DGraph to be examined.

I’ve come across a case where the JSON contains to two fields with the same name but different types i.e. in the case below state is both an object and a string.


{
    Name : "Test",
    Monitoring : {
        State : "disabled"
    },
   State :  {
       Code : 16,
       Name : "running"
   }
}

When I submit this to DGraph I get an error ‘Input for predicate State of type scalar is uid’. Is there a specific way to deal with this - is there any way to ensure objects would be prefixed as they get submitted to Dgraph?

As “Monitoring” and “State” in your JSON are obviously Nodes. We have the “State” predicate set to UID. Therefore you can not perform a mutation for the “State” predicate with another Type. What I recommend is that you create another predicate (Alias) and then use an Alias in the query.

Nops, It is almost impossible to predict what users intend with a JSON obj. It would need an AI to handle it.

Mutation

{
  "set": [
    {
    "Name" : "Test",
    "Monitoring" : {
         "State_Monitoring" : "disabled"
  		  },
    "State" :  {
          "Name" : "running"
   }
   }
  ]
}

Query


{
  q(func: has(Monitoring)) {
    uid
    Name 
    Monitoring { uid State:State_Monitoring }
    State {uid Name}
  }
}

Result


{
  "data": {
    "q": [
      {
        "uid": "0x8",
        "Name": "Test",
        "Monitoring": [
          {
            "uid": "0x9",
            "State": "disabled"
          }
        ],
        "State": [
          {
            "uid": "0x7",
            "Name": "running"
          }
        ]
      }
    ]
  }

Cheers.

Hi MichelDiz, thanks I figured that was the only option. I was hoping Dgraph had something built in, but I can just pre-process the json to deal with this.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.