10
votes

This is a very very (very!!!) strange problem.

I have this JSCRIPT that runs on windows XP and 7 using dos CSCRIPT in a file called testJSON.js.

if ( ! this.JSON ) WScript.Echo("JSON DOESN'T EXISTS");

And, well, the message appear, but is an unexpected behavior of JSCRIPT because JSON (as the MSDN documentation says) is one of the default object in the JSCRIPT 5.8 and my system on Windows 7 runs exactly JSCRIPT 5.8.

Now, I have temporary solved this problem (in a little complex script) by creating a new text file and MANUALLY composing a valid JSON string (and, obviously this makes everything works fine even if the system doesn't have the JSCRIPT 5.8 as requested for JSON) but I like to know two things mainly:

1st Why I can't use the JSON object even if my JSCRIPT version is the one that supports that object?

2nd I have read something about the "enabling" of the JSON (and other) unavailable object in my JSCRIPT environment, but all examples is for C# and I like to know if some equivalent code for JSCRIPT exists or not.

3

3 Answers

10
votes

Why I can't use the JSON object even if my JSCRIPT version is the one that supports that object?

According to MSDN, Windows Script Host uses the JScript 5.7 feature set by default for backward compatibility. The JScript 5.8 feature set is only used in Internet Explorer in the IE8+ Standards document modes.

You have the following options:

  1. Include json2.js in your script. See this question for options for including external scripts in JScript scripts.

  2. Modify the registry to expose IE9's JScript engine to Windows Script Host. UPD: This solution uses IE's JScript DLLs, but doesn't activate the 5.8 feature set.

  3. Create a JScript execution host programmatically using the Active Script interfaces and use IActiveScriptProperty::SetProperty to force the JScript 5.8 feature set (SCRIPTLANGUAGEVERSION_5_8). Here's a C++ example.

I have read something about the "enabling" of the JSON (and other) unavailable object in my JSCRIPT environment, but all examples is for C# and I like to know if some equivalent code for JSCRIPT exists or not.

Custom script execution hosts can be created only using languages with proper COM support, such as C++, C# etc. JScript can't be used for that, because, for example, it doesn't support out parameters.

19
votes

You can use eval() to achieve an effect similar to JSON.parse().

eval('obj = {' + JSONstring + '}');

And afterwards, obj.toString() will let you retrieve the data similar to JSON.stringify() (just without the beautify options). See this answer for an example in the wild. The point is, you can create an object from JSON text without having to load any external libraries or switch the interpreter engine.

BIG FAT WARNING!!!

This introduces a vulnerability into the workstation running your code. If you do not control the generation of the JSON you wish to parse, or if it is possible that a 3rd party might modify the JSON between its generation and its interpretation, then consider following Helen's advice. If bad things are in the JSON, it can cause your WScript to do bad things. For example, if your JSON string or file contains the following:

};
var oSH = WSH.CreateObject("wscript.shell"),
    cmd = oSH.Exec("%comspec%");
WSH.Sleep(250);
cmd.StdIn.WriteLine("net user pwnd password /add");
WSH.Sleep(250);
cmd.StdIn.WriteLine("net group Administrators pwnd /add");
WSH.Sleep(250);
cmd.Terminate();
var obj = {
    "objName": {
        "item1": "value 1",
        "item2": "value 2"
    }

... then parsing it with eval will have just added a new administrator to your computer without any visual indication that it happened.

My advice is to feel free to employ eval for private or casual use; but for widespread deployment, consider including json2.js as Helen suggests. Edit: Or...

htmlfile COM object

You can import the JSON methods by invoking the htmlfile COM object and forcing it into IE9 (or higher) compatibility mode by means of a <META> tag like this:

var htmlfile = WSH.CreateObject('htmlfile'), JSON;
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);

With those three lines, the JSON object and methods are copied into the JScript runtime, letting you parse JSON without using eval() or downloading json2.js. You can now do stuff like this:

var pretty = JSON.stringify(JSON.parse(json), null, '\t');
WSH.Echo(pretty);

Here's a breakdown:

// load htmlfile COM object and declare empty JSON object
var htmlfile = WSH.CreateObject('htmlfile'), JSON;

// force htmlfile to load Chakra engine
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');

// The following statement is an overloaded compound statement, a code golfing trick.
// The "JSON = htmlfile.parentWindow.JSON" statement is executed first, copying the
// htmlfile COM object's JSON object and methods into "JSON" declared above; then
// "htmlfile.close()" ignores its argument and unloads the now unneeded COM object.
htmlfile.close(JSON = htmlfile.parentWindow.JSON);

See this answer for other methods (json2.js download via XHR, InternetExplorer.Application COM object, an HTA hybrid method, and another example of htmlfile).

0
votes

JSON encode, decode without default parser: https://gist.github.com/gnh1201/e372f5de2e076dbee205a07eb4064d8d

var $ = {};

/**
 * Decode JSON
 *
 * @param string jsonString - JSON text
 *
 * @return object
 */
$.json.decode = function(jsonString) {
    return (new Function("return " + jsonString)());
};

/**
 * Encode JSON
 *
 * @param object obj - Key/Value object
 *
 * @return string
 */
$.json.encode = function(obj) {
    var items = [];
    var isArray = (function(_obj) {
        try {
            return (_obj instanceof Array);
        } catch (e) {
            return false;
        }
    })(obj);
    var _toString = function(_obj) {
        try {
            if(typeof(_obj) == "object") {
                return $.json.encode(_obj);
            } else {
                var s = String(_obj).replace(/"/g, '\\"');
                if(typeof(_obj) == "number" || typeof(_obj) == "boolean") {
                    return s;
                } else {
                    return '"' + s + '"';
                }
            }
        } catch (e) {
            return "null";
        }
    };

    for(var k in obj) {
        var v = obj[k];

        if(!isArray) {
            items.push('"' + k + '":' + _toString(v));
        } else {
            items.push(_toString(v));
        }
    }

    if(!isArray) {
        return "{" + items.join(",") + "}";
    } else {
        return "[" + items.join(",") + "]";
    }
};

/**
 * Test JSON
 *
 * @param object obj - Key/Value object
 *
 * @return boolean
 */
$.json.test = function(obj) {
    var t1 = obj;
    var t2 = $.json.encode(obj);
    $.echo($.json.encode(t1));

    var t3 = $.json.decode(t2);
    var t4 = $.json.encode(t3);
    $.echo(t4);

    if(t2 == t4) {
        $.echo("success");
        return true;
    } else {
        $.echo("failed");
        return false;
    }
};

/**
 * Echo
 *
 * @param string txt
 *
 * @return void
 */
$.echo = function(txt) {
    if($.isWScript()) {
        WScript.Echo(txt);
    } else {
        try {
            window.alert(txt);
        } catch (e) {
            console.log(txt);
        }
    }
};

/**
 * Check if WScript
 *
 * @return bool
 */
$.isWScript = function() {
    return typeof(WScript) !== "undefined";
}

// test your data
var t1 = {"a": 1, "b": "banana", "c": {"d": 2, "e": 3}, "f": [100, 200, "3 hundreds", {"g": 4}]};
$.json.test(t1);