0

I have the following json input file( I know it's not a good structure, but I cannot change it)

{
  "TEST1": {
              "ONE": "value",
              "TWO": "value-1",
           },
  "TEST2": [
             {
               "A":  [
                       "test": "test"
                     ],
               "B-B":[
                       "test": "test"
                     ]
             }
          ]
}

And I want to process it in batch loop. But sometimes there are special characters inside which needs to be escaped.

for /f %%i in ('type %LOCAL_PATH_FILE% ^| %JQ% -r ".TEST2[]  | keys[]"') do (
    set SUB=%%i
    ECHO  sub is !SUB!
    for /f %%j in ('type %LOCAL_PATH_FILE% ^| %JQ% -r ".TEST2[].!SUB![] | keys[]"') do (
        set CONTENT=%%j
        ECHO  content path is !CONTENT!
    )
)

The current result looks like:

jq: error: syntax error, unexpected IDENT, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
.TEST2[].B-B[] | keys[]
jq: 1 compile error
The process tried to write to a nonexistent pipe.

I know it should somehow possible to escape it via quota, but I'm not able to find the correct syntax for it.

Maybe someone can help me :)

knittl
  • 246,190
  • 53
  • 318
  • 364
Ric
  • 1
  • I would not use batch files to process json formatted files. It can go haywire really quickly.. Powershell would be waaay better. [see here](https://stackoverflow.com/questions/49136148/how-to-parse-json-response-in-powershell) – Gerhard Apr 01 '21 at 10:55
  • 1
    If you can't change the structure, then there's nothing else you can do, because it simply isn't valid JSON. `["test":"test"]` should either be a proper array `["test","test"]`, or a proper object `{"test":"test"}`. And you should remove the comma after `"value-1"`. Btw, nothing wrong with using the right tools to process JSON in `cmd`. – Reino Apr 01 '21 at 20:40

0 Answers0