| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
Copy of my post to 'alt.msdos.batch.nt'
---------------------------------------------------
To interact with web service API: Sending JSON data by cURL program which output another JSON as result of execution in for loop, and I want to capture list of (possible) errors from resulted JSON. Piping output of cURL into Xidel (JSON parsing utility) which output text string from JSON based on preset filter. Findstr is filtering out possible non-json output preceeding JSON data.
Problem is that JSON contain commas which are considered as delimiter inside FOR loop, so I decide to insert one more pipe of 'sed' (or maybe 'tr') to insert escaping caret before each comma, but I have no luck with this.
Anybody have idea how to escape commas in the JSON output?
for /f "tokens=*" %%a in ('
echo {^
"name":"Faktura"^,^
"proforma":"true"^,^
"subject_id":"%fid%"^,^
"send_email":"true"^,^
"currency": "USD"^,^
"variable_symbol": "%varsym%"^,^
"payment_method": "bank"^,^
"due": 14^,^
"vat_price_mode": "without_vat"^,^
"lines": [^
{^
"name": "Predplatne"^,^
"quantity": "1.0"^,^
"unit_name": "h"^,^
"unit_price": "%fee%"^
}^
]^
} ^| utilities\curl.exe ^
--include ^
--header "user-agent: Moje Aplikace" ^
--header "content-type: application/json; charset=utf-8" ^
--insecure ^
--request POST ^
-u "%login%:%passwd%" ^
-so - ^
--data-binary "@-" ^
--url "%url%%slug%/invoices.json"') do (
for /f "tokens=1,* delims=:" %%A in ('
echo %%a ^| sed.exe -e "s/,/^,/g" ^| findstr /C:"{" ^| utilities\xidel.exe --data^=- -q --dot-notation^=on -e "$json.errors()"
') do (set errors=%%A)
)
------------------------------------------------------------------
>
>
>
>
>
> Which windows version do you use?
> Commandline syntax inside a console window or inside a batch file is
> different.
> To make things more difficult, commandline syntax to declare a for loop
> inside a console window is different to one declared in a batch file.
> Furthermore it would be interesting to know, if you use sed in the
> declaration line of the loop [ eg. for /n %%a in ('xxx |sed
> "DoSomething"') Do (DoSomething)] or if you use sed INSIDE the loop [eg.
> for /n %%a in ('xxx') Do (echo x|sed "DoSomething")]. Behaviour here is
> different too.
>
> Would you provide a code example of your desired for loop?
> And again, should it be executed via console window or inside a batch file?
>
> Am 01.05.2015 um 22:39 schrieb Petr Lázňovský [email protected] [sed-users]:
>>> AG [U [Fri, 1 May 2015 12:46:39 -0700]:
>> >> PLLC[U [Fri, 1 May 2015 12:38:42 -0700]:
>> >> > How do I add caret '^' before each comma ',' in text string? Trying
>> >> > this:
>> >> >
>> >> > echo {"errors":{"subject_id":["mandatory","Contact not exist."]}} |
>> >> > sed.exe -e "s/,/^,/g"
>> >> [...]
>> >> Try enclosing the entire string in double-quotes or single-quotes,
>> >> something like echo "("errors":...}}" | sed ...
>> >> -Amarendra
>>> [...]
>>> Actually, not needed. Your first form works as-is on my W2K8 server
>>> with GNU sed version 4.2.1.
>> I see, it work from cmdline but problem seems arise in FOR loop usage.
>> Complex issue just posted into alt.msdos.batch.nt if someone interested.
>> Thank you anyway, L.
>> ------------------------------------
>> Posted by: =?windows-1250?Q?Petr_L=E1z=F2ovsk=FD?= <[email protected]>
>> ------------------------------------
>
>
>
>