Handling of null in JSON sourced from webread

16 views (last 30 days)
I am attempting to import data contained in a JSON from a RESTful service which works fine using webread by itself, however some of the data cells contain JSON 'null' which seems to be either ignored or handled as an empty cell, which is then subsequently removed (both null and the cell containing it) and results in a mismatch of array sizes.
I wish to download said data and vertically concatenate the arrays containing data, but as their length is inconsistent due to the null values and MATLAB's handling of null values (discarding them) I cannot. The nature of the data means I cannot add these cells back with a NaN value to maintain array dimensions as the position of the null value is inconsistent.
Is there a way to change MATLAB's handling of null values in JSON files grabbed using webread?
  • JSON does not support IEEE 754 NaN/inf values
  • Apparently MATLAB doesn't understand the null value that JSON does support (I'd expect null to be translated to NaN, or supported natively).
Any other solution to this problem would also be welcome, but I believe the best way would be to change how webread deals with the nulls, but the documentation and support is limited, and the functions for grabbing and decoding the data are hidden (and not revealing when you open them).

Accepted Answer

Ben
Ben on 29 Aug 2016
This has been confirmed as a bug by someone from Mathworks Tech support. I have attached a sample data set that exhibits this error, which can be loaded using:
a = fileread('json2.json');
and then decoded with the same decoder as used in webread using:
out = matlab.internal.webservices.fromJSON(a)
which will have the size mismatched arrays in out.results.series.values (cells 50, 84 and 85).
There is a fairly simple workaround, however. Specify the content type in weboptions, which will create the same 'a' as downloading the JSON manually and using fileread.
a = webread(URL, weboptions('ContentType','text'));
The null values can then be replaced by NaN using strrep and decoded:
a = strrep(a,'null','NaN');
out = matlab.internal.webservices.fromJSON(a);
This will give a correctly sized 88x18 double matrix (instead of 88 cells containing double matrices) in out.results.series.values with NaN at the right positions.
  3 Comments
Robert Snoeberger
Robert Snoeberger on 1 Sep 2016
Section 9 of the RFC 7159 JSON standard says, "a JSON parser MAY accept non-JSON forms or extensions." So, matlab may accept the string '{"x":NaN}' if it is considered a non-JSON form or extension.
Guillaume
Guillaume on 1 Sep 2016
Yes, it may accept non-JSON forms, but it definitively SHOULD NOT generate non-valid JSON.
As per section 10 in your link, " A JSON generator produces JSON text. The resulting text MUST strictly conform to the JSON grammar."
Something, that for some reason, I've got a hard time convincing mathworks of.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 26 Aug 2016
Matlab does understand null, for example, in r2016a,
>>json = matlab.internal.webservices.fromJSON('{"values":[1, "2", null]}')
>>json.values
ans =
[1]
'2'
[]
correctly returns a cell array with 3 entries, the last one being empty.
The problem appears to be when the array exclusive contains numbers and null matlab converts it to a matrix, which of course has no concept of null values.
It's impossible to see the code used to parse the json (it's a mex file), but I assume it first read all the data as a cell array. Check if the cell array contains only numeric values and then use cell2mat to convert it into a matrix if it does. Unfortunately, isnumeric is true for empty arrays and cell2mat simply ignores empty values.
At present, short of writing your own decoder, there is no workaround. I'll submit a bug request to mathworks.
  1 Comment
Ben
Ben on 26 Aug 2016
Edited: Ben on 29 Aug 2016
The confirmation that it doesn't inherently have a problem with null values, but rather the fact that the destination array is a double array is both good and annoying to hear. Unfortunately the source JSON definitely has nulls mixed in with a mostly numerical array (see attached sample JSON)
IMO it's a pretty obvious substitution that when it comes to decoding mostly numerical arrays; substitute anything that isn't a number with NaN (that's what NaN is for!).
I also submitted a service request. (Update: got a response and confirmation).

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!