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...
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
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 This is nonsense. A function is here to perform an operation. Transforming it into a string means nothing.
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.
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 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.
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.
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.)
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