Decode JSON into only struct array

Hi,
I am using jsondecode function in matlab to decode nested json script. I want the final output in struct array only. When some child keys are missing, I get a cell array. However, I would like to have all the keys in struct array and if it the child key is not present, I want that key with empty value. Is this function foreseen to have this functionality? is there any fast alternaive to achieve it?
Thanks

5 Comments

Do you know in advance what the format of your struct is?
Your requirements are a bit unclear. The JSON that generates a structure would be completely different from the one that generates a cell array.
>>jsondecode('{"a":"b", "c":"d"}') %generate a structure with two fields
ans =
struct with fields:
a: 'b'
c: 'd'
>>jsondecode('{"a":["b", "e"], "c":["d", "f"]}') %the [] in the json generates a cell array
ans =
struct with fields:
a: {2×1 cell}
c: {2×1 cell}
The two JSON strings are completely different and it wouldn't make much sense to generate a structure from ["b", "e"]
Can you clarify what you want, possibly with some example JSON.
Yes I know the format of my structure array, however I want to keep it flexible to evolve.
Please accept my apology for not being clear enough.
Below code gives a structure array
>> jsondecode('{"a":[{"b":2},{"b":3}]}')
ans =
struct with fields:
a: [2×1 struct]
whereas in this case we get a cell array
>> jsondecode('{"a":[{"b":2},{"b":3,"c":4}]}')
ans =
struct with fields:
a: {2×1 cell}
I undrestand the logic here, however I would prefer a structure array with following format
>> ans.a(1).c
ans =
[]
>> ans.a(2).c
ans =
4
Please let me know if it is still not clear.
Shouldn't the JSON string not be like the one below? Then the string itself would encode an empty element.
jsondecode('{"a":[{"b":2,"c":[]},{"b":3,"c":4}]}')
Alternatively, you could write a parser that ensures every field has the same subfields.
This is what I am doing right now. The problem is that I am dealing with many json files with nested structure, and if I have to add a new key, I have to update all my files manually to avoid cell array.
If no one can help, then the final plan is to write my own parser. Thanks anyway.

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 2 Jul 2019
Edited: Guillaume on 2 Jul 2019
jsondecode is certainly never going to convert a json array into a structure array as there's no guarantee that the objects in the array are of the same type. In your example, the two structures created don't have the same fields.
You can of course, write your own function that merges structures with different fields, filling the missing fields with [] or '' or whatever:
function structarray = mergedissimilarstructures(structures, defaultempty)
%structures: a cell array of scalar structures to merge
%defaultempy: the value to use to fill missing fields. Optional, default = []
%structarray: a structure array the same size as the input structures cell array.
% The fields of structurarray is the union of the fields of the input structures
%TODO: input validation
if nargin < 2
defaultempty = [];
end
fieldunion = cellfun(@fieldnames, structures, 'UniformOutput', false);
fieldunion = unique(vertcat(fieldunion{:}));
structarray = repmat({defaultempty}, numel(structures), numel(fieldunion));
for sidx = 1:numel(structures)
[~, destcol] = ismember(fieldnames(structures{sidx}), fieldunion);
structarray(sidx, destcol) = struct2cell(structures{sidx});
end
structarray = reshape(cell2struct(structarray, fieldunion, 2), size(structures));
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!