# Send dgraph query response as JSON instead of Protocol Buffer

**URL:** <https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856>\
**Category:** Users\
**Created:** [September 12, 2016, 9:34am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856 "2016-09-12T09:34:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul-Sagore](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rahul-sagore/32/1021_2.png) [@Rahul-Sagore](https://discuss.dgraph.io/u/Rahul-Sagore)\
**Post date:** [September 12, 2016, 9:34am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/1 "2016-09-12T09:34:44Z")

</div>

@akhiltak - After executing dgraph query, it return response with \*graph.response type. It should return string so that I can convert it into valid JSON.

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [September 12, 2016, 9:45am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/2 "2016-09-12T09:45:59Z")

</div>

So @Rahul-Sagore it would still return response as `graph.Response`. The properties of the node can have a string value if there is a schema defined. That is what I was referring to. @akhiltak can help you with a schema file for Gru after which you would get the correct types in the response.

---

<div class="post-metadata">

**Author:** ![Rahul-Sagore](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rahul-sagore/32/1021_2.png) [@Rahul-Sagore](https://discuss.dgraph.io/u/Rahul-Sagore)\
**Post date:** [September 12, 2016, 9:53am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/3 "2016-09-12T09:53:49Z")

</div>

Okay, thanks. My Bad.

This is the response I was getting in postman:

```auto
{
  "n": {
    "uid": 3386310919693814617,
    "attribute": "root",
    "properties": [
      {
        "prop": "name",
        "val": "QWxnb3JpdGhtIDI="
      },
      {
        "prop": "_uid_"
      }
    ]
  },
}

```

Which is in a bit wierd format. I thought the format will get fixed and will come as:

```auto
{
  "root": {
    "uid": 3386310919693814617,
    "name": "Algorithm"
  }
}

```

As per the wiki. It is easier to use.

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [September 12, 2016, 10:06am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/4 "2016-09-12T10:06:13Z")

</div>

Hmm, so the format at the bottom that you are referring to is the format that the HTTP Server responds in which is JSON.

The above format is the one in which the gRPC server responds(to which the Go Client talks to). Sorry for the confusion, its inevitable as all examples on Wiki are for the HTTP Client. Regarding the val being base64 encoded, I think that can be taken care of if we have a schema. Could you tell me how do you convert the response from the server to JSON?

---

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [September 12, 2016, 10:52am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/5 "2016-09-12T10:52:03Z")

</div>

If you need JSON, then you could just directly use the /query interface, instead of using the Go client. It responds back in a much more friendly JSON than what you’d get by converting protobuf to JSON.

Also, don’t block on schema. It’s a work in progress, and it’s only going to give you what you can already custom write.

---

<div class="post-metadata">

**Author:** ![Rahul-Sagore](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rahul-sagore/32/1021_2.png) [@Rahul-Sagore](https://discuss.dgraph.io/u/Rahul-Sagore)\
**Post date:** [September 12, 2016, 11:08am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/6 "2016-09-12T11:08:15Z")

</div>

@pawan I hoped that the response will come as second json example, but it was like the first one.

This the code to fetch the tag name with UID and then converting it to json:

```auto
 q := "{root(_uid_: 11905117936582384613 ) { name _uid_ }}"
resp, err := c.Query(context.Background(), &graph.Request{Query: q})

jsonResp, err := json.Marshal(resp)

w.Header().Set("Content-Type", "application/json")
w.Write(jsonResp) //Writing json response to postman request

```

Above code **outputs** :

```auto
{
  "n": {
    "uid": 3386310919693814617,
    "attribute": "root",
    "properties": [
      {
        "prop": "name",
        "val": "QWxnb3JpdGhtIDI="
      },
      {
        "prop": "_uid_"
      }
    ]
  },
}

```

All the tag properties I asked is coming in array of object, with **prop** and **val** keys. Now if I have to get the **tag name** , I have to loop through the array and see check **if prop == “name”** (On UI side), which is a lot more calculation if have more properties.

@mrjn - I’ll try by hitting /query

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [September 12, 2016, 11:25am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/7 "2016-09-12T11:25:26Z")

</div>

Yup try the `/query` endpoint and use that if it works better for you.

---

<div class="post-metadata">

**Author:** ![Rahul-Sagore](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rahul-sagore/32/1021_2.png) [@Rahul-Sagore](https://discuss.dgraph.io/u/Rahul-Sagore)\
**Post date:** [September 13, 2016, 3:59am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/8 "2016-09-13T03:59:25Z")

</div>

I have changed the code to fetch data from /query endpoint. I got this result in postman:

```auto
"{\"root\":{\"_uid_\":\"0x6cc3f59d7dec1ed6\",\"name\":\"Algorithm 2\"}}"

```

This doesn’t look good in postman due to escaping double qoute by slash. But this the perfect format for response, which I wanted. I can parse it on Frontend to use it, thanks.

Just make sure, the uid has correct format, I am fetching data with UID something like “9025164383225914740”, but in above response it is different.

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [September 13, 2016, 4:15am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/9 "2016-09-13T04:15:05Z")

</div>

That’s great. Actually, you can just use the base 16 encoded uid(0x6cc3f59d7dec1ed6) that you get in the response.

The Go Client was returning it in the internal format(uint64) which should be changed.

---

<div class="post-metadata">

**Author:** ![akhiltak](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/akhiltak/32/1018_2.png) [@akhiltak](https://discuss.dgraph.io/u/akhiltak)\
**Post date:** [September 13, 2016, 12:42pm UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/10 "2016-09-13T12:42:34Z")

</div>

Hi @Rahul-Sagore,

If you are using the /query endpoint, you could use a schema file as an input while running the server to get appropriate types like this:

1. Create a schema file.
2. Run dgraph server with addition argument:

- ./dgraph … -schema \< schema-filepath \>

**Schema file(\*.json):**

```auto
{
	"_uid_": "id",
	"name": "string",
	"age": "int"
}

```

Where the key-value pair represents _predicate_ and it’s _type_ respectively.

This would return a response like this:

```auto
        "root": {
          "_uid_": "0x6cc3f59d7dec1ed6",
          "age": 23,
          "name": "Rahul",
          "human": true
        }

```

Note that:

- This will still be a json encoded string, so you will have to decode it at frontend
- ID has type _string_, as per GraphQL spec
- Currently only scalar types are supported
- No type coercion is currently being done in mutations, so you could potentially store _wrong_ types (but this validation will be merged soon)
- Scalar types you could use are: **id** , **string** , **int** , **float** and **bool**

Please do let me know if you have any doubts regarding this.

PS: Apologies for the tardy response.  
PPS: Will be adding this to wiki as soon as type validation is added to mutations.

---

<div class="post-metadata">

**Author:** ![Rader](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rader/32/1061_2.png) [@Rader](https://discuss.dgraph.io/u/Rader)\
**Post date:** [March 14, 2017, 1:38am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/11 "2017-03-14T01:38:46Z")

</div>

do you mean the “[http://server](http://server):port/query?” interface? if so, which means we need to use http instead of grpc in TCP, right?

Or do you have a query interface through grpc?

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [March 14, 2017, 1:57am UTC](https://discuss.dgraph.io/t/send-dgraph-query-response-as-json-instead-of-protocol-buffer/856/12 "2017-03-14T01:57:17Z")

</div>

Thats right @Rader.

To get the response back as JSON you have to use `http://server:port/query` interface. gRPC using the Go client would return result as Protocol Buffers.
