How to pass single element JSON arrays using webwrite
4 views (last 30 days)
Show older comments
Hi,
The question in brief:
"Can anyone advise how to pass single element JSON arrays using webwrite, and how to have them interpreted as integers rather than doubles?"
The problem:
I'm trying to pass a JSON value to an external API, over which I have no control.
The API wants to see:
{
"method": "blahblah",
"params": [1],
"id": 1,
"version": "1.0"
}
Which is confirmed to work. I know a single value array is maybe a bit silly. But it is what is required. And the API rejects a single number value without the square brackets.
In order to pass that to the external API, I am using webwrite
method = 'blahblah';
params = [1];
id = 1;
version = '1.0';
data = struct('method',method,'params',params, 'id',id,'version',version);
options = weboptions('MediaType','application/json');
response = webwrite('URL',data,options)
This generates the following JSON output:
{"method":"blahblah","params":1,"id":1,"version":"1.0"}
As you can see, everything is great except for the single element array value for "params", which is just a number instead of an array.
i.e
"params":1 % which I don't want (doesn't work)
instead of
"params": [1] % which I do want (works)
Drilling down, the matlab-json converter used by webwrite is
mls.internal.toJSON
We can see the function writes JSON arrays when it is passed arrays (although it converts them to doubles, which my API doesn't like at all):
>> mls.internal.toJSON(struct('params',[2,3]))
ans =
{"params":[2.0000000000000000,3.0000000000000000]}
But when passed a single value, it reverts to passing a simple JSON number, rather than a single element array:
>> mls.internal.toJSON(struct('params',[2]))
ans =
{"params":2}
It looks like similar problems have existed elsewhere: (Java) http://stackoverflow.com/questions/17003823/make-jackson-interpret-single-json-object-as-array-with-one-element
Can anyone advise how to pass single element JSON arrays using webwrite, and how to have them interpreted as integers rather than doubles?
Any assistance hugely appreciated.
0 Comments
Accepted Answer
Guillaume
on 20 May 2015
It looks like using a cell array instead of a matrix solves both of your problems (no '.0000' and always an array):
>>mls.internal.toJSON(struct('method', 'blahblah', 'params', {{2 3}})) %note the double {{ to prevent the cell being interpreted as an array of struct
ans = {"method":"blahblah","params":[2,3]}
>>mls.internal.toJSON(struct('method', 'blahblah', 'params', {{2}}))
ans = {"method":"blahblah","params":[2]}
So, basically use:
data = struct('method',method,'params', {num2cell(params)}, 'id',id,'version',version);
As usual, Mathworks have failed to properly document their API.
2 Comments
Guillaume
on 20 May 2015
Note that the double curly brace is required because it's inside the struct function. (Otherwise struct maps the cell array into an array of structures with scalar field.
You would not need the double brace, if using field notation:
s1 = struct('a', {2 3 4}) %create structure s1(1).a = 2, s1(2).a = 3 s1(3).a = 4
s2 = struct('a', {{2 3 4}}) %create structure s2.a = {2 3 4}
s3.a = {2 3 4} %just assign cell to field.
More Answers (0)
See Also
Categories
Find more on JSON Format in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!