필터 지우기
필터 지우기

Read a nested json file.

조회 수: 14 (최근 30일)
DuckDuck
DuckDuck 2018년 5월 19일
댓글: Jan 2018년 5월 19일
I have a nested json file with lines of this type:
{"results":{"whatever_name":{"id":77},"another_name":{"id":89},"another_name":{"id":60}},"timestamp":123456}
the number of observations for each line is different, it might be 3 or whatever. I use loadjson to read these lines but i get structures results.whatever_name.id value. What i want to achieve is a table with four columns: name, id, value, timestamp.
Can anyone help me to flatten these kind of json?
  댓글 수: 1
Rik
Rik 2018년 5월 19일
If people want to find this loadjson function: it's on the FEX.
If your file contains lines with a regular pattern, you can simply read the file yourself and get the parameters with textscan. If your file is not so regular, you can always try some recursive function with fieldnames.

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

답변 (1개)

Jan
Jan 2018년 5월 19일
Do you know jsondecode?
str = '{"results":{"whatever_name":{"id":77},"another_name":{"id":89},"another_name":{"id":60}},"timestamp":123456}';
M = jsondecode(str);
It is not trivial to extrapolate the wanted output from an example of a single line only. So please post a meaningful example of the input data.
  댓글 수: 2
DuckDuck
DuckDuck 2018년 5월 19일
jsondecode gives the same result: creates a struct of results with substructs of whatever_name with another nested substract of id and finally a value. what i want is a table with 4 fields.
Jan
Jan 2018년 5월 19일
The structure of the JSON string is clearly defined as list of two variables, while the first is a list of 3 variables. This is not equivalent to a "table with 4 fields". You cannot expect that a JSON parse reorganize the data magically, because this is not the job of parsing. But of course you can convert the data afterwards. Unfortunately I cannot imagine, what the "4 fields" should be. I assume the 1st field is the name: "whatever_name", "another_name", "another_name", the 2nd is the id: 77, 89, 60, and the 3rd field is 3 times the timestamp 123456, 123456, 123456. But what could be the 4th field?
It would be useful, if you explain exactly, what you want as output. Neither the json parser nor the members of the forum can guess this.

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

카테고리

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