Update: Using JSON to mutate on Dgraph. (cURL, Raw HTTP)

PS. This is too old, pay attention that some procedures might be different today.

Since the release of version 1.0.6 of Dgraph you can already do mutation via RAW HTTP and cURL. This is an extension of JSON support.

I’ll show you how to do this in this thread. I will later on to document this. But it’s here for those who come here to search about JSON in the Discuss. Any doubts about it I’m available.

See the reference of this topic:

Syntaxes examples

To mutate:

var m1 = `
	{
		"set": [
			{
				"name": "Alice"
			},
			{
				"name": "Bob"
			}
		]
	}
	`

To delete a predicate:

var m2 = `
	{
		"delete": [
			{
				"uid": "0x1",
				"name": null
			}
		]
	}
	`

To delete a node:

var m2 = `
	{
		"delete": [
			{
				"uid": "0x1"
			}
		]
	}
	`

Using with cURL

I’ll show you two ways to do this via cURL. First you have to configure the Headers. There are two in this case. One to inform Dgraph that is a JSON mutation and another to commit now.

Headers:

-H 'X-Dgraph-MutationType: json' 
-H 'X-Dgraph-CommitNow: true'

Commands:

ps. In order to use jq you need the jq package. In Fedora OS for example you can install it with
" sudo dnf install jq ".

First way:
curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d  $'
{
  "set": [
  {"name": "Alice"},
  {"name": "Bob"}
]
  }' | jq
Second way:
curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d $'
{ "set": 
  [
  { "name": "Michel" }]
  }' | python -m json.tool | less

To delete a node:

curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d  $'
{
  "delete": [
  {"uid": "0xa"}
]
  }' | jq

Cheers.

Doing a List With JSON via cURL and interact with it

Ref: Modifying predicate lists

Schema:

testList: [string] .

Let’s create our List:

curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d  $'
  {
    "set": [
    { "testList": 
    [ "Grape","Apple","Strawberry","Banana","watermelon"] }
    ]
  }' | jq

Resulting:

{
  "data": {
    "myList": [
      {
        "uid": "0xd",
        "testList": [
          "Grape",
          "Apple",
          "Strawberry",
          "Banana",
          "watermelon"
        ]
      }
    ]
  }
}

Let’s then remove our Apple from this list (Remember, It’s case sensitive):

curl -X POST localhost:8080/mutate -H 'X-Dgraph-MutationType: json' -H 'X-Dgraph-CommitNow: true' -d  $'
{
  "delete": [
  { "uid": "0xd",
    "testList": "Apple"}
]
  }' | jq

Result:

{
  "data": {
    "myList": [
      {
        "uid": "0xd",
        "testList": [
          "Grape",
          "Strawberry",
          "Banana",
          "watermelon"
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 8586,
      "processing_ns": 370722,
      "encoding_ns": 710978
    },
    "txn": {
      "start_ts": 59,
      "lin_read": {
        "ids": {
          "1": 29
        }
      }
    }
  }
}