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
Raqeeb Alam
  • 25
  • 1
  • 8

2 Answers2

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>
npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • 1
    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. – rojo Jul 29 '15 at 13:04
  • line 48 invalid character – Raqeeb Alam Jul 29 '15 at 13:15
  • 1
    @rojo - thanks :). Mind the `` 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. – npocmaka Jul 29 '15 at 13:15
  • @RaqeebAlam - have you changed the line `set "jsonFile=.\url.txt"` with the path to your json file? – npocmaka Jul 29 '15 at 13:17
  • @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. – npocmaka Jul 29 '15 at 13:21
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.

Community
  • 1
  • 1
rojo
  • 22,626
  • 5
  • 47
  • 93
  • so you are fixing invalid the content? – npocmaka Jul 29 '15 at 13:28
  • 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\* – rojo Jul 29 '15 at 13:30
  • @RaqeebAlam If either npocmaka's answer or mine was helpful, please consider choosing one to mark as accepted. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. – rojo Jul 30 '15 at 11:50