0

Below is the file content in Json

{
  "FD_sfdsdf:ClientMasterBaseUrl": "http://example.com",
  "FD_fsdfs:CommonAPIBaseUrl": "http://example.com",
  "FD_sdfsadfsdf:Secret": "sdfsdf"
}

Command:

cat fd.json| jq -r 'to_entries[] | "setx \(.key) \"\(.value)\""'

which I run in windows CMD.

Output:

setx FD_sfdsdf:ClientMasterBaseUrl "http://example.com"

Above is the output desired which I am getting in MAC and Linux but the same command doesn't work in windows.

Error:

'"setx \(.key) \"\' is not recognized as an internal or external command, operable program or batch file.
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • How doesn't the command work? What output do you get? – Mokubai Jun 14 '22 at 11:25
  • I get this error `'"setx \(.key) \"\' is not recognized as an internal or external command, operable program or batch file. ` – Adarsh Hiwrale Jun 14 '22 at 11:29
  • You must obey the respective shell quoting rules/syntax. – Daniel B Jun 14 '22 at 11:45
  • I *guess* in CMD single-quotes do not really quote, double-quotes kinda do. Sometimes it may appear single-quotes work, only because programs receive their arguments as one long string and need to do word splitting and quote removal by themselves; so supporting single-quotes is possible *at this stage* if the program supports them. But before this happens, in your case CMD sees unquoted `|`, as if you wanted to *pipe* to `"setx …`. By today standards of \*nix shells, CMD is manure. – Kamil Maciorowski Jun 14 '22 at 11:46
  • related: https://stackoverflow.com/questions/7760545/escape-double-quotes-in-parameter . But a comment tells this might still depend on the Windows parsing library used by the command (here that would be jq): https://stackoverflow.com/questions/7760545/escape-double-quotes-in-parameter#comment28290139_15262019 – A.B Jun 14 '22 at 13:35
  • I found `jq -r "to_entries[] | """set "\(.key)=\(.value)"""" "` works perfectly for me in windows and i get the below output `set FD_sfdsdf:ClientMasterBaseUrl=http://example.com` But I want value in double quotes – Adarsh Hiwrale Jun 16 '22 at 07:56
  • Do you really need to use `cat` and `jq` tools? – Io-oI Jun 16 '22 at 16:04

1 Answers1

0

1. Do not use : to compose variables names, this character is used inside/next to the variable name for several actions, see who here

2. If the goal is to define a variable getting the strings from your .json file, you could just use cmd...


  • In your bat/cmd file:

    @echo off & cd /d "%~dp0"
    
    for /f tokens^=1-4delims^=^:^"^,^  %%i in ('
         type file.json^|find /i "FD_sfdsdf"')do setx FD_sfdsdf_%%~j "\"%%~k:%%~l\""
    
  • In your command line:

    for /f tokens^=1-4delims^=^:^"^,^  %i in ('type file.json^|find /i "FD_sfdsdf"')do setx FD_sfdsdf_%~j "\"%~k:%~l\""
    

  • Outputs:

    enter image description here

  • Obs.: 1. You need two spaces (spacespace), one is used as a delimiter along with the other characters, and the other is in respect of the for /f loop syntax:

    for /f tokens^=1-4delims^=^:^"^,^spacespace%%i

  • Obs.: 2. For double quotes just add \" and being escaped in the windows registry.:

    ...)do setx FD_sfdsdf_%%~j "\"%%~k:%%~l\""

Io-oI
  • 7,588
  • 3
  • 12
  • 41