Se si salva una tabella con valori vuoti in una delle colonne numeriche in questo modo:
New Table( "testEmpty",
Add Rows( 2 ),
New Column( "Col1",
Character,
"Nominal",
Set Selected,
Set Values( {"test", "test"} )
),
New Column( "Col2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [., .] )
)
)come file JSON, risulterà in questo:
[
{
"Col1" : "test",
"Col2" : .
},
{
"Col1" : "test",
"Col2" : .
}
]Sembra che non tutti siano d'accordo sul fatto che questa sia una sintassi JSON valida. Se rileggo questo file in JSON usando, diciamo, Powershell:
$json = Get-Content -Path 'C:\Users\myUser\testEmpty.json' -ErrorAction Stop | ConvertFrom-Json
verrà visualizzato un errore:
ConvertFrom-Json : Invalid JSON primitive: ..
At C:\Users\myUser\testEmpty.ps1:1 char:118
+ ... \myUser\testEmpty.json' -ErrorAction Stop | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
Qualche idea su come risolvere questo problema? Eventuali opzioni durante il salvataggio di file JSON utilizzando script? O l'unica soluzione è assicurarsi che la tabella non abbia valori vuoti?
Sono abbastanza sicuro che dovrebbero essere modificati in valori nulli per essere JSON valido.
Se hai solo due colonne puoi provare a convertire datatable in array associativo e quello in JSON (NOTA: Associative Array non consentirà chiavi duplicate. JSON valido lo fa, ma non è sicuro di quanto sarebbe bello lavorare con chiavi duplicate)
Names Default To Here(1);
dt = New Table( "testEmpty",
Add Rows( 2 ),
New Column( "Col1",
Character,
"Nominal",
Set Selected,
Set Values( {"test", "test1"} )
),
New Column( "Col2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [., .] )
)
);
aa = Associative Array(Column(dt, "Col1"), Column(dt, "Col2"));
json = As JSON Expr(aa);Questo sarebbe simile a questo:
"{\!" test \! ": null, \!" test1 \! ": null}"
e se lo salvi con Salva file di testo ("c: \ temp \ test.json", json) rimuoverà quelle virgolette con escape JMP {"test": null, "test1": null}