JSON.parse can not be used in win11 ltsc 2024?

	var look = function(){return{
        "1": "1.0",
        "2": "2.0"
        // do not put , in the last line
    }
    }.toString().slice(17, -3);
	JSON.parse(look)

On win11 version: 23H2 22631.3737 , the js code above does not report error.

but on win version:LTSC 2024 26100.1742 , the code report error:
Error at line 7, position 2 (raw line 7)
invalid character (0x800a03f6)
Any one can tell me why? please...

What are you trying to do ?

This looks very strange : you have a function supposedly returning an object, but you then transform it into a string (slicing the function part) to then try and transform it back to an object ...

If you try and output the string you ask JSON.parse to transform, it will show :

{
        "1": "1.0",
        "2": "2.0"
        // do not put , in the last line
    }

And this is not a valid JSON string (no comment possible in JSON).

What I would have done (in a button) :

function OnClick(clickData)
{

	var myObject = GetJSobject();
	var myObjectAsString = JSON.stringify(myObject);
	dout("obj as string = '" + myObjectAsString + "'");
	var backToObj = JSON.parse(myObjectAsString);

	var v = { "toto":"AAA", "titi":"BBB" };
	dout("v toto = " + v.toto);

}

function dout(msg) { DOpus.Output(msg); }

function GetJSobject() { 
	return  { "1": "1.0", "2": "2.0" };
}

Note that if you want to access members of your JS object with the dot (.) notation, as in the example at the end with var v= ..., you need to use identifiers that do not start with a number (replacing the "1" and "2").

Note : Edited to change the code and finalizing message ... sthg went wrong :slight_smile:

1 Like

I know what you mean.
My problem is this script:

this script failed to initialize on win11 LTSC 2024, but works normally on Win11 23H2 and earlier
I don‘t know why....

You can test some code on win11 ltsc 2024.

function log(str){DOpus.Output(str,false,true);}

var fields_base_reference = function(){return{
        // This is just the help variable for your reference
        "MExt_HasMetadata"                : "essential",
        "MExt_NeedsUpdate"                : "essential",
        "MExt_TotalBitrate"               : "essential",
        "MExt_VideoCodec"                 : "essential"
    }
    }.toString().slice(17, -3);
	var dd=JSON.parse(fields_base_reference)
	var cc=JSON.stringify(dd)
	log(dd.MExt_VideoCodec)

I'm not on Win11 LTSC 2024, so I won't be able to test.
You should wait for an answer on the other trhead.

What I can say, is that transforming a function (not its return) into a string, slicing it to try and transform it into what looks like the string representation of a JScript object to then try and parse it to make it an object is very complicated and is not the way to do things.

If you want to try a simple example to see if there is an issue with JSON.parse, you should try the code I posted initialy. Does it work ?
If it does, then it means the problem comes from elsewhere and JSON.parse is working, at least to some extent.

Finally I found it is the regex string..
if I replace it with "nameOnly": ["aaa", " "] , then no problem.
I don''t why it cannot work on win11 ltsc 2024.

    var name_cleanup = function(){return{
        "nameOnly": ["/(_|\.)/g",  " "]
    }
    }.toString().slice(17, -3);
    JSON.stringify(JSON.parse(name_cleanup)); // test parseability on script load, do not remove

You should definitely strop trying to convert functions to strings :slight_smile: This is nonsense. A function is here to perform an operation. Transforming it into a string means nothing.

Instead of

 var name_cleanup = function(){return{
        "nameOnly": ["/(_|\.)/g",  " "]
    }
    }.toString().slice(17, -3);

do

 var name_cleanup = '{"nameOnly": ["/(_|\.)/g",  " "] }';
1 Like

Thank you very much! I have solved the problem.

1 Like

how have you solved the issue ?

and can you explain why the issue didn't happen on windows 11 23H2 ?

It's never been said, but AFAIC the first issue was with the way JSON.parse was used, e.g. applied to a function converted to a string, and modified to remove unwanted parts ...

So, IHMO, if you're also having an issue with JSON.parse, you're better off creating your own thread with the detailed description of what you're experiencing (including the code and the error you get).
Linking your account could also be a good idea.

This JSON object, is it even part of JScript?

I remember I had to inject / create a JSON object on my own to get parsing / stringifying to work in JScript (Win7 + Win10).

It is usable at least for static methods as JSON.parse and JSON.stringify

What that block, with the function...toString().slice() does is extracting the body of a dummy "function". Why was it needed you ask, you can create JSON objects with comments, which is normally not allowed, which you can in turn show in script help and internally use as json :wink: Why and how are well documented in the Mediainfo-script source.

In fact, I use function...toString() in few other places to extract the name of a caller function, because JScript is stupid and does not support .caller and alike.

Maybe DO started to inject this JSON object on its own at some point?
It is not part of regular JScript:

Really, stupid? o) Maybe it's just not the ECMAScript version or something you expect. It also does not seem to be a valid standard property or seems deprecated? Not sure, I don't do much modern day javascript.

I don't think it's us doing it. Microsoft seem to have been changing JScript in the last few years, FWIW, so it may have come from that.

That's weird.. where is it from then? o)
If you did not add it to the scripting context, I really wonder where it comes from. As shown above, when running a *.js script with cscript.exe in a "regular" JScript context, it is unknown "JSON is undefined".

If somebody finds the JSON origin somewhere, please report back! o)

JSON has been in JScript long before Win11 & 22H2 Upgrade or whenever it was; I've been using it for years now in DOpus scripts. Maybe it is available only in certain contexts, like cscript.exe environment doesn't but COM object does.

My guess is that cscript defaults to the "base" version of the JScript interpreter, which is 5.7. Opus sets it to 5.8 by default, which may be where the JSON object comes from.

(Using the JSCRIPTVER comment in Opus you can set it to an even later version.)

1 Like

Wait a second, I thought JSCRIPTVER was meant to be used only with 3 or nothing.

I just put // !JSCRIPTVER=11 on top a script file and could use newer syntax like let a=1, const c=2, "use strict"; etc. These do not work witth // !JSCRIPTVER=3 or without any // !JSCRIPTVER line, only with // !JSCRIPTVER=11.

First I thought this was huge, because other syntax like class could be used but unfortunately class or even trivial stuff like let eight=2**3 cause syntax errors. I've tested a few things from ES5 upwards (see Wikipedia ES history) and most of them cause syntax errors. All in all // !JSCRIPTVER=11 also has very little impact :pensive:

None of this is really documented, I only stumbled upon 3 working by accident.

These are the "documented" values in the Windows header files:

Opus sets the version to 2 by default.

1 Like