亲爱的社区成员,
一旦我们解析 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 列表。
查看更多...
字典被转换为关联数组,并且 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 适合的话,它可能就像使用最佳猜测选项一样简单。