親愛的社區成員,
一旦我們解析 json,程序就知道其所有級別的所有鍵。我們可以純粹根據位置提取值,還是每次都必須獲取所有不同級別的密鑰?
例如:
Names Default To Here( 1 );
l = Parse JSON(
"[ { \!"name\!": \!"KATIE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 59, \!"weight\!": 95 }, { \!"name\!": \!"LOUISE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 61, \!"weight\!": 123 }, { \!"name\!": \!"JANE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 55, \!"weight\!": 74 } ]"
);
For( i = 1, i <= Length( l ), i++,
Show( l[i] );
); // works just fine
<BR />
For( i = 1, i <= Length( l ), i++,
Show( l[i] );
For( ii = 1, ii <= Length( l[i] ), ii++,
Show( l[i][ii] ); // not working
);
);
l[1]["age"]; // brings "12" without getting the keys of l[1]
l[1][1]; // not working - subscripted key not in map
keys = l[1] << get keys;
l[1][keys[1]]; // brings 12 works fine
For( i = 1, i <= Length( l ), i++,
keys = l[i] << get keys; // can we skip this step?
For( ii = 1, ii <= Length( keys ), ii++,
Show( l[i], l[i][keys[ii]] )
);
);
非常感謝!
謝謝@Craige_Hales
實際上我的 json 解析有一些問題。 我從數據庫中獲取了數百萬個數據,因此我使用 sql 將它們全部下載到 jmp 數據表中。 然後,一一解析,提取出我需要的信息。我想為每個孩子保留一行,並為每個孩子復制該人的所有信息。
考慮以下腳本。這是使用嚮導手動生成的。由於子項的名稱位於標題中,因此默認情況下嚮導不會聚合它們。如果我重複“孩子”這個詞而不是蒂什、咪咪和伯塔,它會使用長猜測完美地猜出它。
我該如何從那裡開始使其適應 json 可能具有的任意數量的不同子項?如何更改 tish、mimi 和 berta 的硬編碼?另外,一些孩子的屬性可能會丟失,所以我需要能夠跳過它們。
JSON To Data Table(
"\[
{
"Person": "sami",
"Age": "45",
"event": "came home",
"when": 1639682643.7996492,
"Children": {
"tish": [
{
"Name": "tish",
"Father": "bob",
"Age": "14",
"event": "went to school",
"when": 1639682630.1256561
}
],
"mimi": [
{
"Name": "mimi",
"Father": "bob",
"Age": "25",
"event": "went to play",
"when": 1639682630.1256561
}
],
"berta": [
{
"Name": "berta",
"Father": "bob",
"Age": "23",
"event": "went to work",
"when": 1639682630.1256561
}
]
}
}
]\",
JSON Settings(
Stack( 0 ),
Row( "/root/Children/tish" ),
Row( "/root/Children/mimi" ),
Row( "/root/Children/berta" ),
Col(
"/root/Person",
Column Name( "Person" ),
Fill( "Use Forever" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Age",
Column Name( "person Age" ),
Fill( "Use Forever" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/event",
Column Name( "person event" ),
Fill( "Use Forever" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/when",
Column Name( "person when" ),
Fill( "Use Forever" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children",
Column Name( "Children" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/tish/Name",
Column Name( "Name" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/tish/Father",
Column Name( "Father" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/tish/Age",
Column Name( "Age" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/tish/event",
Column Name( "event" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/tish/when",
Column Name( "when" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/mimi/Name",
Column Name( "Name" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/mimi/Father",
Column Name( "Father" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/mimi/Age",
Column Name( "Age" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/mimi/event",
Column Name( "event" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/mimi/when",
Column Name( "when" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/berta/Name",
Column Name( "Name" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/berta/Father",
Column Name( "Father" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/berta/Age",
Column Name( "Age" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/berta/event",
Column Name( "event" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
),
Col(
"/root/Children/berta/when",
Column Name( "when" ),
Fill( "Use Once" ),
Type( "Character" ),
Format( {"Best", 15} ),
Modeling Type( "Continuous" )
)
),
JSON Wizard( 1 )
);
到目前為止,我正在循環遍歷每個級別的密鑰(暴力),但花了三天多的時間才檢查了 600 萬個 json 的樣本。
您的示例數據是 Python 字典的 Python 列表。
查看更多...Python 和 JMP 使用的 { } 和 [ ] 或多或少彼此相反。 Python [ ] 創建一個列表,由數字索引。 JMP 使用 [ ] 表示矩陣或關聯數組。 Python 使用 { } 來創建字典。 JMP 相當於字典的是 [ ] 關聯數組。 JMP 的列表使用 { }。
字典被轉換為關聯數組,並且 JMP 的 AA 不使用這樣的數字下標。 但你可以非常接近。 在 JMP 16 之前,您可以使用 <<
jlist = Parse JSON(
"[
{ \!"name\!": \!"KATIE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 59, \!"weight\!": 95 },
{ \!"name\!": \!"LOUISE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 61, \!"weight\!": 123 },
{ \!"name\!": \!"JANE\!", \!"age\!": 12, \!"sex\!": \!"F\!", \!"height\!": 55, \!"weight\!": 74 }
]"
);
for each({record,index1}, jlist,
show(record,index1); // ... record = ["age" => 12, "height" => 55, "name" => "JANE", "sex" => "F", "weight" => 74]; index1 = 3;
for each({{key,value},index2}, record, show(key,value,index2)); // key = "age";value = 12;index2 = 1;key = "height";value = 55;index2 = 2;...
)
外層對於每個知道它正在處理一個列表,並且內部對於每個知道它正在處理 AA。 我提出第一個論點對於每個在這兩種情況下都非常複雜,因此它可以包含索引 1 和索引 2;您實際上可能不需要數字索引。
您沒有說您的數據實際上是什麼樣子。 您可能希望使用 JSON 導入嚮導創建腳本以將數據直接加載到數據表中。 如果 JSON 適合的話,它可能就像使用最佳猜測選項一樣簡單。