JSON decode - lost information

18 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;
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 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.
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)




Community Treasure Hunt

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

Start Hunting!

Translated by