# How to handle conflicting types in JSON?

**URL:** <https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188>\
**Category:** Dgraph\
**Created:** [August 3, 2020, 3:30pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188 "2020-08-03T15:30:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamalsaha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/tamalsaha/32/4246_2.png) [@tamalsaha](https://discuss.dgraph.io/u/tamalsaha)\
**Post date:** [August 3, 2020, 3:30pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188/1 "2020-08-03T15:30:10Z")

</div>

I am looking to store Kubernetes YAML/JSONs in dgraph. The issue is sometimes fields with same name (predicate in dgraph) have int or string data. Example:

```auto
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

```

```auto
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
    - protocol: TCP
      port: 443
      targetPort: https

```

In Kubernetes, which uses openapi v3, this field will defined using `anyOf`.

```auto
targetPort:
  anyOf:
  - type: integer
  - type: string
  description: Name or number of the port to access on
    the container. Number must be in the range 1 to 65535.
    Name must be an IANA_SVC_NAME.
  x-kubernetes-int-or-string: true

```

Will it be possible to store this types of JSONs in dgraph?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [August 3, 2020, 3:51pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188/2 "2020-08-03T15:51:24Z")

</div>

> [@tamalsaha](#):
>
> Will it be possible to store this types of JSONs in dgraph?

Dgraph can have only one type per predicate. If your predicate switches between two o more types. You should set it as a string.

---

<div class="post-metadata">

**Author:** ![tamalsaha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/tamalsaha/32/4246_2.png) [@tamalsaha](https://discuss.dgraph.io/u/tamalsaha)\
**Post date:** [August 3, 2020, 5:54pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188/4 "2020-08-03T17:54:35Z")

</div>

Can dgraph automatically convert types to string? Or, we are going to get error?

---

<div class="post-metadata">

**Author:** ![ahsan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ahsan/32/3517_2.png) [@ahsan](https://discuss.dgraph.io/u/ahsan)\
**Post date:** [August 3, 2020, 6:50pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188/8 "2020-08-03T18:50:53Z")

</div>

Hey @tamalsaha,

Dgraph would not consider the type to be of “string” by default in case of predicate having different types of values. I apologize for the previous information. It would infer the type based on the value of the predicate. So mutation like this would throw an error.

```auto
{
    "set": [
  		{
        "port": 443 
		},
      {
        "port": "http"
      }
      ]
}

```

For these types of predicates, I suggest you to define the type as “string” beforehand.

---

<div class="post-metadata">

**Author:** ![tamalsaha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/tamalsaha/32/4246_2.png) [@tamalsaha](https://discuss.dgraph.io/u/tamalsaha)\
**Post date:** [August 3, 2020, 7:04pm UTC](https://discuss.dgraph.io/t/how-to-handle-conflicting-types-in-json/9188/9 "2020-08-03T19:04:58Z")

</div>

That is unfortunate given how dgraph handles all predicates in a Global namespace. Now 2 different JSON objects can’t have fields with same name but different datatype.
