將 JMP 表匯出為 JSON在 JSON <=> JMP 中擴展@Craige_Hales 的優秀答案 ,有沒有辦法將 JMP 表轉換為 JSON 文件,以保留原始表的行順序(以方便人類可讀)?使用 JMP 17.2.0。
內容組織資料混合與清理分享和交流成果回覆:將 JMP 表匯出為 JSON該 JMP 表應變更為哪種格式

New Table("Untitled",
Add Rows(5),
Compress File When Saved(1),
New Column("name",
Character,
"Nominal",
Set Values({"KATIE", "LOUISE", "JANE", "JACLYN", "LILLIE"})
),
New Column("age",
Numeric,
"Ordinal",
Format("Fixed Dec", 5, 0),
Set Values([12, 12, 12, 12, 12])
),
New Column("sex",
Character(1),
"Nominal",
Set Values({"F", "F", "F", "F", "F"})
),
New Column("height",
Numeric,
"Continuous",
Format("Fixed Dec", 5, 0),
Set Values([59, 61, 55, 66, 52])
),
New Column("weight",
Numeric,
"Continuous",
Format("Fixed Dec", 5, 0),
Set Values([95, 123, 74, 145, 64])
)
)

[
{
"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
},
{
"name" : "JACLYN",
"age" : 12,
"sex" : "F",
"height" : 66,
"weight" : 145
},
{
"name" : "LILLIE",
"age" : 12,
"sex" : "F",
"height" : 52,
"weight" : 64
}
]
行順序不是根據清單中項目的順序保留的嗎?
回覆:將 JMP 表匯出為 JSON嗨@jthi ,
感謝您的回覆。我應該包含更多細節。關聯數組中不保留鍵順序。例如,在下面的腳本中不保留性別順序。
我想這個結果需要一個全新的解決方案。我很好奇是否有人能夠弄清楚。
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Sort( Replace Table, By( :sex ), Order( Descending ) );
result = [=> ];
For( i = 1, i <= N Rows( dt ), i += 1,
If( !Contains( result, dt:sex[i] ),
result[dt:sex[i]] = [=> ]
);
If( !Contains( result[dt:sex[i]], dt:age[i] ),
result[dt:sex[i]][dt:age[i]] = [=> ]
);
If( !Contains( result[dt:sex[i]][dt:age[i]], dt:name[i] ),
result[dt:sex[i]][dt:age[i]][dt:name[i]] = [=> ]
);
result[dt:sex[i]][dt:age[i]][dt:name[i]]["height"] = dt:height[i];
result[dt:sex[i]][dt:age[i]][dt:name[i]]["weight"] = dt:weight[i];
);
Write( As JSON Expr( result ) );
回覆:將 JMP 表匯出為 JSON我仍然不確定您正在尋找的格式,我可以看到腳本創建的內容(如果我沒記錯的話,這不是有效的JSON,因為鍵不是字串),但您希望基本上根據某些內容對它們進行分組(在這種情況下,性別 -> 年齡 -> 姓名,它們是鍵,然後姓名有一個帶有更多鍵的值)。你到底想做什麼?
回覆:將 JMP 表匯出為 JSON輸出應如下所示,但“F”和“M”的順序應相反。
{
"F": {
12: {
"JACLYN": {
"height": 66,
"weight": 145
},
"JANE": {
"height": 55,
"weight": 74
},
"KATIE": {
"height": 59,
"weight": 95
},
"LILLIE": {
"height": 52,
"weight": 64
},
"LOUISE": {
"height": 61,
"weight": 123
}
},
13: {
"ALICE": {
"height": 61,
"weight": 107
},
"BARBARA": {
"height": 60,
"weight": 112
},
"SUSAN": {
"height": 56,
"weight": 67
}
},
14: {
"CAROL": {
"height": 63,
"weight": 84
},
"ELIZABETH": {
"height": 62,
"weight": 91
},
"JUDY": {
"height": 61,
"weight": 81
},
"LESLIE": {
"height": 65,
"weight": 142
},
"PATTY": {
"height": 62,
"weight": 85
}
},
15: {
"AMY": {
"height": 64,
"weight": 112
},
"MARY": {
"height": 62,
"weight": 92
}
},
16: {
"MARION": {
"height": 60,
"weight": 115
},
"MARTHA": {
"height": 65,
"weight": 112
}
},
17: {
"LINDA": {
"height": 62,
"weight": 116
}
}
},
"M": {
12: {
"JAMES": {
"height": 61,
"weight": 128
},
"ROBERT": {
"height": 51,
"weight": 79
},
"TIM": {
"height": 60,
"weight": 84
}
},
13: {
"DAVID": {
"height": 59,
"weight": 79
},
"JOE": {
"height": 63,
"weight": 105
},
"JOHN": {
"height": 65,
"weight": 98
},
"MICHAEL": {
"height": 58,
"weight": 95
}
},
14: {
"ALFRED": {
"height": 64,
"weight": 99
},
"CHRIS": {
"height": 64,
"weight": 99
},
"EDWARD": {
"height": 68,
"weight": 112
},
"FREDERICK": {
"height": 63,
"weight": 93
},
"HENRY": {
"height": 65,
"weight": 119
},
"JEFFREY": {
"height": 69,
"weight": 113
},
"LEWIS": {
"height": 64,
"weight": 92
}
},
15: {
"CLAY": {
"height": 66,
"weight": 105
},
"DANNY": {
"height": 66,
"weight": 106
},
"MARK": {
"height": 62,
"weight": 104
},
"ROBERT": {
"height": 67,
"weight": 128
},
"WILLIAM": {
"height": 65,
"weight": 111
}
},
16: {
"PHILLIP": {
"height": 68,
"weight": 128
}
},
17: {
"KIRK": {
"height": 68,
"weight": 134
},
"LAWRENCE": {
"height": 70,
"weight": 172
}
}
}
}回覆:將 JMP 表匯出為 JSON在您的範例中,名稱的順序也發生了變化。只有最外層必須保持特定的順序嗎?
我認為直接從 JMP 創建 JSON 的任何現有方法都不支援這樣的格式,並且由於 JMP 沒有像 Python 中的 OrderedDict 這樣的東西,因此鍵將始終進行排序。如果您有 JMP18,您也許可以利用 Python 整合。純粹在 JMP/JSL 中,我認為您將依賴某些部分來建立字串。 JSL 中的其他選項是,您很可能會為資料添加額外資訊以強制排序、建立關聯數組、從中建立 JSON 字串(尚未儲存),然後使用 Substitute 將這些值替換回原始值。
編輯:您也可以建立 JSON 字串,然後在其中執行交換。這是使用替代品的非常簡單的範例
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
dt << Sort(Replace Table, By(:sex), Order(Descending));
replacements = ["F" => "$002$", "M" => "$001$"];
:sex << Set Each Value(replacements[:sex]);
result = [=> ];
For(i = 1, i <= N Rows(dt), i += 1,
If(!Contains(result, dt:sex[i]),
result[dt:sex[i]] = [=> ]
);
If(!Contains(result[dt:sex[i]], dt:age[i]),
result[dt:sex[i]][dt:age[i]] = [=> ]
);
If(!Contains(result[dt:sex[i]][dt:age[i]], dt:name[i]),
result[dt:sex[i]][dt:age[i]][dt:name[i]] = [=> ]
);
result[dt:sex[i]][dt:age[i]][dt:name[i]]["height"] = dt:height[i];
result[dt:sex[i]][dt:age[i]][dt:name[i]]["weight"] = dt:weight[i];
);
json_str = Substitute(As JSON Expr(result), replacements << get values, replacements << get keys);
Write(json_str);
這將需要進行大量更改以使其更加健壯和動態(創建替換鍵的功能,使用子集來避免操縱原始資料/為此利用列屬性)
回覆:將 JMP 表匯出為 JSON不久前我正在研究 JMP 到 JSON 腳本。這是它當前的迭代。至於在沒有字母鍵順序的情況下對 JSON 檔案進行排序(基於值順序屬性?),我認為這將需要另一層複雜性,以增量步驟編寫 JSON 表達式,而不是依賴「作為 JSON Expr ()”函數。
另外,我還沒有編寫 JSON 到 JMP 腳本來將其轉換回資料表。我在這裡使用的模式是我自己開發的模式。
Names Default To Here( 1 );
dt = Current Data Table();
aa = Associative Array();
aa["name"] = dt << Get Name( "Ignore Extension" );
// get table variables
tvarnames = dt << get table variable names;
If( N Items( tvarnames ),
aa["table variables"] = Associative Array();
For Each( {tvar, i}, tvarnames,
Insert Into( aa["table variables"], tvar, dt << get table variable( tvar ) )
);
);
// get table scripts
tscriptnames = dt << get table script names;
If( N Items( tscriptnames ),
aa["table scripts"] = Associative Array();
For Each( {tscript, i}, tscriptnames,
// tscript = "DOE Dialog"
Insert Into( aa["table scripts"], tscript, Char( dt << get script( tscript ) ) )
);
);
// get script group names
script_groups = dt << get script groups names;
// iterate through each script group and get a list of the script in the group, add that to the associative array as a character string
If( N Items( script_groups ),
aa["script groups"] = Associative Array();
For Each( {scrgroup, i}, script_groups,
//scrgroup="FIT Y BY X"
Insert Into( aa["script groups"], scrgroup, Char( dt << get script group( scrgroup ) ) )
);
);
// capture row states
// captured as a string representation, because as json expr() doesn't handle associative array elements that are lists or matrices very well
aa["row states"] = Char( dt << get row states );
// get labeled columns
// note that the jmp data table stores these as a table property and not as a property for each column
aa["label columns"] = Char( dt << get label columns );
// get columns, column properties and data
aa["columns"] = Associative Array();
For( i = 1, i <= N Cols( dt ), i++,
col = Column( dt, i );
colname = col << get name;
// create column entry
aa["columns"][colname] = Associative Array();
// column order
aa["columns"][colname]["column order"]=i;
// modeling type
aa["columns"][colname]["modeling type"] = col << get modeling type;
// data type
aa["columns"][colname]["data type"] = col << get data type;
// format
aa["columns"][colname]["format"] = col << get format;
// get column properties
colproperties = col << get properties list;
If( N Items( colproperties ),
// note that << get properties list returns a list of expressions, converting them to character
For( k = 1, k <= N Items( colproperties ), k++,
colproperties[k] = Char( colproperties[k] )
);
//add properties element
aa["columns"][colname]["properties"] = Associative Array();
//add element for each property, capturing as character string to avoid expression eval issues
For Each( {prop, k}, colproperties, aa["columns"][colname]["properties"][prop] = Char( col << get property( prop ) ) );
);
// get data
aa["columns"][colname]["data"] = As List( col << get values );
);
Show( aa );
nw = New Window( "Complete Data Table as JSON", sb = Text Box( char(As JSON Expr( aa )), <<SET wrap="">" if you want to save a text file ; file(path || "jmpdt json.json", as json expr(aa), mode("replace")); * ode></SET>回覆:將 JMP 表匯出為 JSON謝謝。效果很好。