0

I want a extract a url from a json file and store that into the variable using the batch script.

Below is my json file.

 {
  "url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
  "html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
  "assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
  "upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
  "tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
  "zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
  "id": 1,
  "node_id": "MDc6UmVsZWFzZTE=",
  "tag_name": "v1.0.0",
  "target_commitish": "master",
 }

I want the line between "upload_url" and {?name,label}" stored into the variable. which is

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets

Can I get the batch script command for achieving this?

I tried the below command:

for /f "tokens=2 delims=:" %%# in (' type tmpCurl.json^|find /i "upload_url"') do echo %%#

but it give this much output:

"https
0

3 Answers 3

1

This should be what you require:

@echo off
for /f "tokens=1,* delims=:" %%a in ('type tmpCurl.json ^| findstr /i "upload_url"') do (
    for /f "tokens=1,* delims={" %%i in (%%b) do set "result=%%i"
)
echo %result%

The first loop splits the string on : delimeter and assigns everything before : on to metavariable %%a leaving you with everything below assigned to %%b

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",

The second loop splits the string %%b on { and assigns everything before { to %%i leaving us with:

https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets
Sign up to request clarification or add additional context in comments.

3 Comments

Now the output is https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}","upload_url":"https://uploads.github.com/repos/octocat/Hello-World/releases /1/assets I want https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets Can you please help me to get that?
The very latest code is what I wanted. Thanks a lot. Also can you please put the explainaton as well?
Explanation added.
0

Based upon your provided .json content something like this may be sufficient for your :

@For /F Tokens^=3Delims^={^"^  %%A In ('Find "upload_url"^<"tmpCurl.json"')Do @Set "UURL=%%A"
@Set UURL 2>Nul&Pause

Where the second line is there just to show you the saved variable with its value.

Comments

0

Your sample Json is invalid due to the trailing comma in the 2nd last line.
Better use Json tools/script languages which can handle Json.

A PowerShell one liner wrapped in batch

:: Q:\Test\2019\05\18\SO_56197577.cmd
@Echo off
For /f "Usebackq delims=" %%U in (`powershell -NoP -C ^
  "(Get-Content tmpCurl.json | ConvertFrom-Json).upload_url.split('{')[0]"
`) Do Set "upload_url=%%U"
set upload_url

Sample output

> Q:\Test\2019\05\18\SO_56197577.cmd
upload_url=https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets

Comments

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.