0

I have a unique issue where string data from an JSON array field doesn't get interpreted by Postman at runtime. The field 'providers' is combined into one string instead of individual strings.

What is the best way to escape string data to get the output desired like --> "providers": ["prov-a", "prov-b", "prov-c"]

JSON:

   {
        "field-a": "askfhkfhksjfh",
        "field-b": "south32qa",
        "providers": ["prov-a", "prov-b", "prov-c"]
    }

POST API BODY:

{
    "name": "name-A",
    "id": "name-id",
    "providers": ["{{providers}}"]
}

Actual body data created by postman @runtime:

{
    "name": "name-A",
    "id": "name-id",
    "providers": ["prov-a, prov-b, prov-c"]  <-- issue here!!
}
Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

0

Here's a batch way to transform the string

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "postman={ "name": "name-A", "id": "name-id", "providers": ["prov-a, prov-b, prov-c"]}"

FOR /f "tokens=1,2,*delims=[]" %%u IN ("%postman%") DO (
 SET "postman=%%~v"
 SET "postman=%%u["!postman: =","!"]%%w"
)

ECHO postman=%postman%

GOTO :EOF

The for separates the string of interest between the [ and ]. assigning the part before the [ to %%u, that after ] to %%w and the string to %%v. In order to manipulate %%v, it needs to be assigned to a user variable. %%~v removes the enclosing quotes Then re-string %%u and %%w with the brackets and replace the spaces in (%%v) with "," and quote the result.

This worked for me in W10. What I find intriguing is that the commas logically should appear in %%v but do not.

Revision -------------

In the light of the newest revision of the "actual" code produced,

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q73031428.txt"

(    
FOR /f "usebackqtokens=1,2,*delims=[]" %%u IN ("%filename1%") DO IF "%%~v"=="" (
 ECHO %%u
) ELSE (
 SET "postman=%%~v"
 SET "postman=!postman:,=!"
 ECHO %%u["!postman: =","!"]%%w
)
)>replacementfilename

GOTO :EOF

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

I've assumed the code is available in file q73031428.txt

Really, it's the same code but revised since the newest layout available contains newlines where the original didn't.

Intriguingly also, %%v this time includes commas, hence they're removed in the new version.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • @jeb : Seems to be a question that might bear investigating. The commas do not appear in `%%v`, which surprised me.... – Magoo Jul 19 '22 at 06:47
  • Try `for /F "usebackq delims=" %I in ('a,b;=c') do @echo/%I` in Command Prompt – you will find that the output is `a b c` rather than `a,b;=c`, which proves that the portion behind `in` becomes somehow reparsed, which should be a result of the special `for` parsing as explained in [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/a/4095133) Note that `,`, `;` and `=` are standard token separators just like the _space_, which are *not* protected by quotation marks here; so so are the `,` in the portion `prov-a, prov-b, prov-c` of this question/answer… – aschipfl Jul 19 '22 at 12:08
  • *N. B.:* As far as I remember, mentioning someone (by `@`) not involved in a thread does not notify them… – aschipfl Jul 19 '22 at 12:10
  • @aschipfl : quite likely. Perhaps time to set up a chat room for batchers. And this time `%%v` has its commas intact... – Magoo Jul 20 '22 at 00:00