1

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%
        }]}
2
  • Sure might just not be the most productive method tough. Do you want to create the file or just replace some lines? Commented Jul 18, 2014 at 20:44
  • I am trying to change some value like change the value of field1 Commented Jul 29, 2014 at 15:03

1 Answer 1

1

Not possible for an internal command, because it doesn't understand JSON, but you can use the JSON-parser instead.

Creating a new JSON object

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.

Editing a JSON document

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.