How i can add a List to the JSON POST request with webwrite (required for RESTful interface of Neo4j)

2 views (last 30 days)
The following RESTful JSON Interface is needed to submit a Query (e.g. Node creation) to a Neo4j-Database:
{
"statements" : [ {
"statement" : "CREATE (n) RETURN id(n)"
} ]
}
The JSON-Object is created internal the WEBWRITE-Function. The nested structure can be created in Matlab and is compiled to JSON by the WEBWRITE-Function as well. I was unable to create a JSON List (Array) by using the following approach (Cell Array):
api = 'http://localhost:7474/';
url = [api 'db/data/transaction/commit'];
statem{1,1} = 1;
ab = struct('statement', 'CREATE (n) RETURN id(n)');
statem{1,1} = ab;
statems = struct('statements', statem);
options = weboptions('ContentType','json', 'MediaType','application/json', 'RequestMethod','POST', 'Username', 'neo4j', 'Password', 'test');
data = webwrite(url, statems, options);
JSON-Result created by the function above:
{
"statements": {
"statement": "CREATE (n) RETURN id(n)"
}
}
Is there a way to create a List in the JSON-Object created by WEBWRITE?

Accepted Answer

Robert Snoeberger
Robert Snoeberger on 15 Dec 2015
Edited: Robert Snoeberger on 15 Dec 2015
To create a JSON Array, you should use a cell array. However, you should be careful when passing a cell array as a value input argument to the function struct [1]. There are a few ways to proceed.
You could avoid the struct function when building the "statements" name-value pair:
>> ab = struct('statement', 'CREATE (n) RETURN id(n)');
>> statems.statements = {ab}
statems =
statements: {[1x1 struct]}
>>
You could add an extra pair of curly braces '{}' when the value input argument to struct is a cell array:
>> ab = struct('statement', 'CREATE (n) RETURN id(n)');
>> statems = struct('statements', {{ab}})
statems =
statements: {[1x1 struct]}
>>
Just make sure that the display of the variable statems includes a pair of curly braces '{}' around [1x1 struct]:
statems =
statements: {[1x1 struct]}

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!