2

I am fiddling with some a script for Fiddler, which uses JScript.NET. I have a string of the format:

{"params":{"key1":"somevalue","key2":"someothervalue","key3":"whatevervalue", ...

I want to match and show "key2":"someothervalue" where someothervalue could be any value but the key is static.

Using good old sed and bash I can replace the part I am looking for with:

$ a='{"params":{"key1":"somevalue","key2":"someothervalue","key3":"whatevervalue", ...'
$ echo $a | sed -r 's/"key2":"[^"]+"/replaced/g'
{"params":{"key1":"somevalue",replaced,"key3":"whatevervalue", ...

Now. Instead of replacing it, I want to extract that part into a variable using JScript.NET. How can that be done?

2
  • I don't know much about JScript.NET, but parsing code with regex seems like a bad idea. Can't you load the script as an object? I'm a PHP man, the equivalent would be json_decode($json) - isn't there an option like this with JScript.NET? Commented Dec 4, 2014 at 13:47
  • It's certainly an alternative. I'd like to learn how to show regexp matches using JScript.NET though, as I will use the same method for showing other interesting stuff. Commented Dec 4, 2014 at 15:20

1 Answer 1

2

The most graceful way is to use a JSON parser. My personal preference is to import IE's JSON parser using the htmlfile COM object.

import System;

var str:String = '{"params":{"key1":"foo","key2":"bar","key3":"baz"}}',
    htmlfile = new ActiveXObject('htmlfile');

// force htmlfile COM object into IE9 compatibility
htmlfile.IHTMLDocument2_write('<meta http-equiv="x-ua-compatible" content="IE=9" />');

// clone JSON object and methods into familiar syntax
var JSON = htmlfile.parentWindow.JSON,

// deserialize your JSON-formatted string
    obj = JSON.parse(str);

// access JSON values as members of a hierarchical object
Console.WriteLine("params.key2 = " + obj.params.key2);

// beautify the JSON
Console.WriteLine(JSON.stringify(obj, null, '\t'));

Compiling, linking, and running results in the following console output:

params.key2 = bar
{
        "params": {
                "key1": "foo",
                "key2": "bar",
                "key3": "baz"
        }
}

Alternatively, there are also at least a couple of .NET namespaces which provide methods to serialize objects into a JSON string, and to deserialize a JSON string into objects. Can't say I'm a fan, though. The ECMAScript notation of JSON.parse() and JSON.stringify() are certainly a lot easier and profoundly less alien than whatever neckbeard madness is going on at Microsoft.


And while I certainly don't recommend scraping JSON (or any other hierarchical markup if it can be helped) as complicated text, JScript.NET will handle a lot of familiar Javascript methods and objects, including regex objects and regex replacements on strings.

sed syntax:

echo $a | sed -r 's/("key2"):"[^"]*"/\1:"replaced"/g'

JScript.NET syntax:

print(a.replace(/("key2"):"[^"]*"/, '$1:"replaced"'));

JScript.NET, just like JScript and JavaScript, also allows for calling a lambda function for the replacement.

print(
    a.replace(
        /"(key2)":"([^"]*)"/,

        // $0 = full match; $1 = (key2); $2 = ([^"]*)
        function($0, $1, $2):String {
            var replace:String = $2.toUpperCase();
            return '"$1":"' + replace + '"';
        }
    )
);

... Or to extract the value of key2 using the RegExp object's exec() method:

var extracted:String = /"key2":"([^"]*)"/.exec(a)[1];
print(extracted);

Just be careful with that, though, as retrieving element [1] of the result of exec() will cause an index-out-of-range exception if there is no match. Might either want to if (/"key2":/.test(a)) or add a try...catch. Or better yet, just do what I said earlier and deserialize your JSON into an object.

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

2 Comments

Not working for me if i use htmlfile.parentWindow.JSON before close it returns undefined
@MamdouhFreelance Did you do the http-equiv="x-ua-compatible" stuff and set compatibility to "IE=9"? By default, JScript and JScript.NET load an older interpreter, one that does not include the JSON object and methods. You must use the meta tag to force JScript.NET to load the Chakra engine. Also, if you're using JScript through Windows Scripting Host and not the .NET flavor of JScript, you have to use comObj.write rather than the comObj.IHTMLDocument2_write method demonstrated above. If this does not solve your problem, please create a new question post with full details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.