wondering if it possible for a batch command to go into a json file and set values. For example, Json File
{ "JsonSample" : [{
"Field1": %Value From Batch Command%,
"Field2": %Value From Batch Command%
}]}
wondering if it possible for a batch command to go into a json file and set values. For example, Json File
{ "JsonSample" : [{
"Field1": %Value From Batch Command%,
"Field2": %Value From Batch Command%
}]}
Not possible for an internal cmd command, because it doesn't understand JSON, but you can use the JSON-parser xidel instead.
SET var1=Value1
SET var2=Value "2"
With environment-variable():
xidel -se "{'JsonSample':array{{'Field1':environment-variable('var1'),'Field2':environment-variable('var2')}}}"
xidel -se ^"^
{^
'JsonSample':array{^
{^
'Field1':environment-variable('var1'),^
'Field2':environment-variable('var2')^
}^
}^
}^
"
With --variable=<string>:
xidel -s --variable="var1" --variable="var2"^
-e "{'JsonSample':array{{'Field1':$var1,'Field2':$var2}}}"
Output in both cases:
{
"JsonSample": [
{
"Field1": "Value1",
"Field2": "Value \"2\""
}
]
}
Also notice how Xidel properly escapes the double-quotes.
SET var1=Value1
SET var2=Value "2"
xidel -s --variable="var1" --variable="var2" "input.json"^
-e "($json).JsonSample(1).Field1:=$var1,($json).JsonSample(1).Field2:=$var2" # dot notation
-e "$json('JsonSample')(1)('Field1'):=$var1,$json('JsonSample')(1)('Field2'):=$var2" # JSONiq notation
-e "$json?JsonSample?1?Field1:=$var1,$json?JsonSample?1?Field2:=$var2" # XPath 3.1 syntax
{
"JsonSample": [
{
"Field1": "Value1",
"Field2": "Value \"2\""
}
]
}
Instead of --variable=<string> obviously you can use environment-variable() as well.
The XPath-like notation ($json/(JsonSample)(1)/Field1) can't be used in this case.