viewpoint-particle

Author Topic: JSON.stringify  (Read 59 times)

bottomleypotts

  • Newbie
  • *
  • Posts: 10
JSON.stringify
« on: January 05, 2018, 07:13:57 am »
Was looking through the forum looking for any advances on JSON, alas nothing.

However, I thought I could help out with this old post: http://forum.winbatch.com/index.php?topic=981

It seems MS jscript does not have the stringify function built in. I ran into this when I wanted to extract an array from an object and got a link back instead of text, when using the original udfJSONget.

So here is my new code:

Code: Winbatch
#DefineFunction JSONplus(strJSONData, strJSONKey) ; udfGetJSONValue
If strJSONData==`` Then Return ``
objJSC=CreateObject(`MSScriptControl.ScriptControl`)
objJSC.Language=`JScript`
js=`var JSON;JSON||(JSON={}),(function(){"use strict";function i(n){return n<10?"0"+n:n}function f(n){return o.lastIndex=0,o.test(n)?'"'+n.replace(o,function(n){var t=s[n];return typeof t=="string"?t:"\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+n+'"'}function r(i,e){var h,l,c,a,v=n,s,o=e[i];o&&typeof o=="object"&&typeof o.toJSON=="function"&&(o=o.toJSON(i)),typeof t=="function"&&(o=t.call(e,i,o));switch(typeof o){case"string":return f(o);case"number":return isFinite(o)?String(o):"null";case"boolean":case"null":return String(o);case"object":if(!o)return"null";n+=u,s=[];if(Object.prototype.toString.apply(o)==="[object Array]"){for(a=o.length,h=0;h<a;h+=1)s[h]=r(h,o)||"null";return c=s.length===0?"[]":n?"[\n"+n+s.join(",\n"+n)+"\n"+v+"]":"["+s.join(",")+"]",n=v,c}if(t&&typeof t=="object")for(a=t.length,h=0;h<a;h+=1)typeof t[h]=="string"&&(l=t[h],c=r(l,o),c&&s.push(f(l)+(n?": ":":")+c));else for(l in o)Object.prototype.hasOwnProperty.call(o,l)&&(c=r(l,o),c&&s.push(f(l)+(n?": ":":")+c));return c=s.length===0?"{}":n?"{\n"+n+s.join(",\n"+n)+"\n"+v+"}":"{"+s.join(",")+"}",n=v,c}}typeof Date.prototype.toJSON!="function"&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+i(this.getUTCMonth()+1)+"-"+i(this.getUTCDate())+"T"+i(this.getUTCHours())+":"+i(this.getUTCMinutes())+":"+i(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});`
js=js:`var e=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,o=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,n,u,s={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},t;typeof JSON.stringify!="function"&&(JSON.stringify=function(i,f,e){var o;n="",u="";if(typeof e=="number")for(o=0;o<e;o+=1)u+=" ";else typeof e=="string"&&(u=e);t=f;if(f&&typeof f!="function"&&(typeof f!="object"||typeof f.length!="number"))throw new Error("JSON.stringify");return r("",{"":i})}),typeof JSON.parse!="function"&&(JSON.parse=function(n,t){function r(n,i){var f,e,u=n[i];if(u&&typeof u=="object")for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(e=r(u,f),e!==undefined?u[f]=e:delete u[f]);return t.call(n,i,u)}var i;n=String(n),e.lastIndex=0,e.test(n)&&(n=n.replace(e,function(n){return"\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(n.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return i=eval("("+n+")"),typeof t=="function"?r({"":i},""):i;throw new SyntaxError("JSON.parse");})})();`
js=js:`var obj=`:strJSONData:`; JSON.stringify(obj.`:strJSONKey:`);`
Ret=objJSC.Eval(js)
Return StrCat(Ret)
#EndFunction

Jim, you will find that you can modify your variable using something like and it will modify your variable and return a string (obviously if you haven't found a better solution; is anyone working on a better solution in winbatchworld because this method slows my code execution down):

Code: Winbatch
js=js:`var obj=`:strJSONData:`; obj.`:strJSONmod:`; JSON.stringify(obj);`

Hope this helps someone!

JTaylor

  • Pundit
  • *****
  • Posts: 812
    • Data & Stuff Inc.
Re: JSON.stringify
« Reply #1 on: January 05, 2018, 07:41:52 am »
Thanks.  I ended up using .NET to convert JSON to XML for processing.

Jim

stanl

  • Pundit
  • *****
  • Posts: 682
Re: JSON.stringify
« Reply #2 on: January 05, 2018, 12:25:27 pm »
Thanks.  I ended up using .NET to convert JSON to XML for processing.

Jim

Gee Jim. Seems like only yesterday we used to post threads like that. Like you, I abandoned in favor of Powershell Convert-JSON cmdlet.