3

I need to read a json file:

{
   "key": {
       "subkey": "value"
   }
}

so I can pass it as an argument to a command:

program -e SETTINGS=<JSON FILE AS STRING>
2
  • 1
    Perhaps this is what you want! Commented Sep 16, 2017 at 15:12
  • 1
    Before trying to automate this I'd check if it works with a condensed string like this with escaped inner double quotes program -e "SETTINGS={\"key\":{\"subkey\": \"value\"}}" Commented Sep 16, 2017 at 15:15

1 Answer 1

4

Combining the answers to these questions...

... we can write the following batch file:

:: Make it possible to read immediate value of variable using !variable! syntax.
setlocal enabledelayedexpansion

:: Read file "test.json" into variable data, removing line breaks.
set data=
for /f "delims=" %%x in (test.json) do set "data=!data!%%x"

:: Escape double quotes in data
set data=%data:"=\"%

:: Finally call program with the entire content of the JSON file as parameter
program -e "SETTINGS=%data%"

Note that when using the cmd.exe command processor, the maximum length for a commandline and also for environment variables is 8191 characters, so this will obviously restrict the maximum size of the JSON file you can pass.

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

1 Comment

Your length limit is misleading / not relevant - batch is further restricted to 8191 max length for variables and also for command lines.

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.