1

i need to extract only URL and app id in the given string and saved in variables

url:{ "url":"ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640", "app":61}

final result like

variable_1 : ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640

variable_2 : 61
3
  • is url:{ "url":"ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640", "app":61} variable or data written in text file? Commented Jul 29, 2015 at 12:32
  • a line written in text file Commented Jul 29, 2015 at 12:39
  • @npocmaka i am waiting Commented Jul 29, 2015 at 12:54

2 Answers 2

1

Here's another hybrid solution using JScript. (Still save it with a .bat extension.)

@if (@CodeSection == @Batch) @then

@echo off
setlocal

set "JSONfile=test.json"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%JSONfile%"') do set "%%~I"

setlocal enabledelayedexpansion
echo URL: !url!
echo App: !app!
endlocal

goto :EOF

@end // end batch / begin JScript chimera

var fso = WSH.CreateObject('scripting.filesystemobject'),
    JSONfile = fso.OpenTextFile(WSH.Arguments(0), 1);

eval('obj = {' + JSONfile.ReadAll() + '}');
JSONfile.Close();

function walk(tree) {
    for (var i in tree) {
        if (typeof tree[i] === 'object') walk(tree[i]);
        else WSH.Echo(i + '=' + tree[i]);
    }
}

walk(obj);

Output:

URL: ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640
App: 61

Delayed expansion was used to prevent the & in the URL from being evaluated.

See this big fat warning if you don't control the generation of the JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

so you are fixing invalid the content?
Sorry, I think you lost me. Are you talking about the addition of the braces in the eval line? If so, I suppose I am. *shrug*
1

this works if there's an url.txt file in same directory with content: {"url":{ "url":"ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640", "app":61}} Eventually you'll need to change name/location of the file on the third line (save the file bellow with .bat extension):

@echo off

setlocal enableDelayedExpansion

set "jsonFile=.\url.txt"
set counter=1



for /f %%# in ('echo %jsonFile%^|mshta.exe "%~f0"') do (
  set "variable_!counter!=%%#"
  set /a counter=counter+1
)

set variable_

echo #############################
echo ###  more batch code here ###
echo #############################


exit /b

<HTA:Application
   ShowInTaskbar = no
   WindowsState=Minimize
   SysMenu=No
   ShowInTaskbar=No
   Caption=No
   Border=Thin
>
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<script language="javascript" type="text/javascript">
    window.visible=false;
    window.resizeTo(1,1);

   var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
   var fso2= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
   var json=fso2.ReadLine();

   var fso3=new ActiveXObject("Scripting.FileSystemObject");

   var file = fso3.OpenTextFile(json, 1);

    var strText = file.ReadAll();
    file.Close();

   var obj = JSON.parse(strText)

   fso.Write(obj.url.url + "\r\n" + obj.url.app + "\r\n");
   window.close();
</script>

5 Comments

I'd upvote 27 times if I could. Good for you for parsing JSON as JSON, rather than trying to tokenize and split as text. And I always enjoy your batch + HTA hybrid solutions.
line 48 invalid character
@rojo - thanks :). Mind the <meta http-equiv="x-ua-compatible" content="ie=edge" /> line which enables ecmascript 5 features in mshta (not possible with cscript/jscript). Its also possible to use jscript.net/c# but it will require compilation and .net installed.
@RaqeebAlam - have you changed the line set "jsonFile=.\url.txt" with the path to your json file?
@RaqeebAlam - also have on mind that this will work only with valid json content. Root curly brackets and quotes around non-numeric elements are mandatory.

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.