JSON field name contains '.' character
10 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have an issue when loading certain .json files into MATLAB using data = jsondecode(fileread('file.json')). The representation of the data in MATLAb is a struct, and '.' is reserved to access fields in the struct; however, a field name in the original .json file contains '.' - it is the name of another .json file with its extension, for example 'file2.json'.
In the data struct, this automatically gets converted to file2_json. The problem is that my program is meant to edit file.json and resave it as newfile.json, and this change in field name from file2.json to file2_json persists when encoding the edited data struct back into a .json file. This then renders the new file unusable.
Apart from manually editing newfile.json to correct the error, is there any way in MATLAB to get around this or is it just an unavoidable function of how the data is represented as a struct?
Thanks
2 件のコメント
akshatsood
2023 年 8 月 31 日
Hi Zachary,
I understand that you are facing issue in encoding your data struct back into json. As stated by you, the field 'file2.json' gets converted into 'file2_json'. This happens because the documentation page of struct clearly states that field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and must begin with a letter. In MATLAB, field names cannot contain special characters, including periods (.) and spaces. That justifies the conversion of '.' to '_'. while encoding. As per my understanding, manual editing is the only way possible.
I hope this helps.
Voss
2023 年 8 月 31 日
@Zachary Nairac: It should be possible to have your program edit file.json and resave it as newfile.json, without going through jsondecode/jsonencode, using basic text file reading/writing functions and string/character array manipulation functions. Can you upload a sample .json file (change the extension to .txt first), and describe the modifications your program needs to perform?
回答 (1 件)
Pratyush
2023 年 11 月 14 日
編集済み: Pratyush
2023 年 11 月 14 日
Hi Zachary,
I understand that one of your field in JSON contains a period('.') in its value which gets converted into an underscore('_') when you use "jsondecode" function.
When loading JSON files using "jsondecode", the resulting data is represented as a struct. The issue you are encountering is due to the fact that field names in MATLAB structs cannot contain dots ('.'). When a field name in the original JSON file contains a dot, MATLAB automatically converts it to an underscore ('_'). Refer to this documentation for the above limitation:
To work around this issue, you can use the following steps:
- Load the JSON file using "jsondecode" and obtain the struct representation.
- Access the field containing the file name with a dot using the underscore instead.
- Perform your desired modifications on the struct.
- Before encoding the struct back into a JSON file, rename the field back to its original name by replacing the underscore with a dot.
- Encode the modified struct back into a JSON file using "jsonencode".
2 件のコメント
Joy
2025 年 8 月 18 日
Hello,
Can Step 4 be done in MATLAB? For example, it would be impossible for MATLAB to jsonencode a struct as
"{
"my.field": 5
}"
But is there any other way to get MATLAB to produce that JSON? Another question talked about supporting jsonencode for dictionaries and that would be awesome, I don't see any way to do this in current R2025a.
Thanks
Joy
2025 年 8 月 18 日
Oh, I suppose MATLAB has "readdictionary" and "writedictionary". Those really are only helpful in a very specific case where
1) You want to go to/from a file, which I do not - I want the string itself (and I don't want to create 1000 temporary files)
2) dictionary must be top level within this file. There can be no mixing between structs and dictionaries - for example, no MATLAB function will decode a struct with a field that is dictionary. And you can not writedictionary if your dictionary has values of type struct (or anything besides int/string/etc).
Would be a lot nicer if jsondecode would automatically convert to dictionary, just like how struct arrays become cell arrays of structs when fieldnames are different.
参考
カテゴリ
Help Center および File Exchange で JSON Format についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!