JSON decode - lost information

13 views (last 30 days)
Simon Parten
Simon Parten on 12 Dec 2018
Commented: Wendell Wilkerson on 10 Sep 2021
Let's say I get this string from the serverside;
{"1D2381B":["fd56c0fd-8882-4b8e-9941-0bd04489981e"],"BZ5ZZ65":["2c768773-7d5f-4d06-8fa1-6ce819dc383a"],"11852420":["b8953809-a264-41b3-a1c3-f3592cd51419"]}
I want to be able to parse and use this information. Matlab automagically decodes it... but it 'makes valid names' of my IDs.
As modifying an ID somewhat destroys it's utility. I'd like a way to prevent or otherwise recover the information. The function 'makeValidNames' returns a second argument, which says which entries were modified.
This information is lost in the JSON decode function. Can anyone help with a workaround, on how I can convert this without the destruction of information?

Accepted Answer

Guillaume
Guillaume on 12 Dec 2018
It's something I've complained about to Mathworks in the early days of jsondecode. In fact, you can use matlab jsonencode to encode a matlab.containers.Map that you would not be able to read back correctly with jsondecode due to that mangling of json keys.
I suggested that jsondecode returns a matlab.containers.Map instead which wouldn't require name mangling and the developer did say that it was something they were considering for a future release. Either they have decided against it or it's yet to be implemented.
I suggest that you raise a service request to Mathworks. The more people complain, the more likely it will change.
In the meantime, I'm afraid you'll have to delegate to a 3rd party parser or write your own. It's not that difficult to parse json actually.
  4 Comments
Guillaume
Guillaume on 12 Dec 2018
You can simplify this slightly by replacing:
raw = fread(fid,inf);
str = char(raw');
by
str = fread(fid, [1 Inf], '*char');
Wendell Wilkerson
Wendell Wilkerson on 10 Sep 2021
+1 Support ticket submitted. I had the same thought about using containers.Map instead of structure to avoid mangling names.
In the mean time, I modified the parse_json function available on the file exchange (https://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser) to use containers.Map instead of structures. It worked for my meager needs.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!