Reading JSON struct from file adds prefix 'x' to fieldname if it starts with number

조회 수: 12 (최근 30일)
I am reading JSON file using readstruct, and it works well otherwise, but for fieldnames that would start with a number there is 'x' added to the beginning. I have tried to search info for this feature, both for Matlab and JSON standard in general, but I cannot find anything that would explain why 'x' is added.
  1. Is this feature only in Matlab, or is it something defined for JSON standard ?
  2. Is there a way to disable this prefix or change it to something else?
Note that I cannot affect to the JSON files how they are originally written.
  댓글 수: 1
Stephen23
Stephen23 2025년 9월 4일
"Field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and must begin with a letter. The maximum length of a field name is namelengthmax."

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2025년 9월 4일
편집: Matt J 2025년 9월 4일
but I cannot find anything that would explain why 'x' is added.
Presumably you know that something must be added, because variable and field names starting with numbers are illegal in Matlab.
>> S.3abc=5
S.3abc=5
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
If you want a different prefix, one option would be to post-process the field names, e.g.,
S.a=2;
S.b=3;
S.x1=7;
S.x2=11
S = struct with fields:
a: 2 b: 3 x1: 7 x2: 11
prefix="y";
F=string(fieldnames(S))';
idx=startsWith(F,"x"+digitsPattern);
F=F(idx);
for k=1:numel(F)
f=F{k};
fnew=prefix+f(2:end);
S.(fnew)=S.(f);
end
S=rmfield(S,F)
S = struct with fields:
a: 2 b: 3 y1: 7 y2: 11
  댓글 수: 6
Steven Lord
Steven Lord 2025년 9월 8일
You'd need to avoid #d..., #e..., #i, and #j.
format shortg
x = [1d2; 1e2; 1i; 1j]
x =
100 + 0i 100 + 0i 0 + 1i 0 + 1i
You'd also need to avoid 0x... and 0b....
y = [0xff; 0b111]
y = 2×1
255 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
There may be other number-letter leading combinations that you'd have to avoid, but it's late here and I can't think of them off the top of my head.
Petri M
Petri M 2025년 9월 8일
@Steven Lord Sure, generality would be an issue. However, I cannot resist the temptation to point out the filosofical question of whether zero is actually a number ;) But please no, don't bother answering this. I know what the answer is in terms of mathematics.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Catalytic
Catalytic 2025년 9월 5일
Note that I cannot affect to the JSON files how they are originally written.
It is easy enough to write a function to modify them -
modifyJSON('music.json', 'newmusic.json', 'ggg')
type music.json
{
"1Ensemble": {
"2Music": "jazz",
"3BandName": "Kool Katz",
"4Instrumentation": [
{
"Type": "wind",
"Instrument": "trumpet",
},
{
"Type": "percussion",
"Instrument": "piano",
"Pianotype": "concert grand",
},
{
"Type": "percussion",
"Instrument": "drums",
"Drumkit": [
"bass drum",
"floor tom",
"snare drum",
"hi-hat",
"ride cymbal"
],
},
{
"Type": "string",
"Instrument": "bass",
"Basstype": "upright"
}
]
},
"5Musicians": [
{
"Role": "trumpeter",
"Name": "Miles"
},
{
"Role": "vocalist",
"Name": "Roger"
},
{
"Role": "pianist",
"Name": "Diana"
},
{
"Role": "drummer",
"Name": "George"
},
{
"Role": "bassist",
"Name": "John"
}
]
}
type newmusic.json
{
"ggg1Ensemble": {
"ggg2Music": "jazz",
"ggg3BandName": "Kool Katz",
"ggg4Instrumentation": [
{
"Type": "wind",
"Instrument": "trumpet",
},
{
"Type": "percussion",
"Instrument": "piano",
"Pianotype": "concert grand",
},
{
"Type": "percussion",
"Instrument": "drums",
"Drumkit": [
"bass drum",
"floor tom",
"snare drum",
"hi-hat",
"ride cymbal"
],
},
{
"Type": "string",
"Instrument": "bass",
"Basstype": "upright"
}
]
},
"ggg5Musicians": [
{
"Role": "trumpeter",
"Name": "Miles"
},
{
"Role": "vocalist",
"Name": "Roger"
},
{
"Role": "pianist",
"Name": "Diana"
},
{
"Role": "drummer",
"Name": "George"
},
{
"Role": "bassist",
"Name": "John"
}
]
}
function modifyJSON(oldfile,newfile, prefix)
arguments
oldfile string, newfile string
prefix string
end
Str=readlines(oldfile);
pat=whitespacePattern+'"'+digitsPattern+alphanumericsPattern+'":';
k=startsWith(Str,pat);
str=Str(k);
for i=1:numel(str)
s=extractBetween(str(i), whitespacePattern+'"', '":');
str(i)=replaceBetween(str(i), whitespacePattern+'"', '":', prefix+s);
end
Str(k)=str;
writelines(Str, newfile);
end
  댓글 수: 1
Matt J
Matt J 2025년 9월 5일
You could also use jsondecode to go directly to a struct if your original file are strictly JSON compliant (e.g., no trailing commas),
outputStruct = jsondecode(strjoin(Str,newline))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by