WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: JTaylor on June 20, 2014, 09:28:29 AM

Title: JSON Text from Object
Post by: JTaylor on June 20, 2014, 09:28:29 AM
I've been using the following from the Tech Database but trying to make a slight tweak.  I can parse the values from JSON but want to update one of the values (which works fine) and then convert the entire Object back to Text (which doesn't).   I can't find any way to get the JSON in text form.   All the examples I can find assume that a "JSON" object is available (or have to include an external library), which I assume webbrowsers support natively, and which contains "stringify" and which seems to be what I want but isn't available via MSScriptControl...at least I can't figure out how I'm supposed to initialize it. 

How can I get the complete JSON data back in text form going this route? 

Thanks.

Jim

Code (winbatch) Select


#DefineFunction JSONGET ( JSON_Data, JSON_Return )

   If StrLen(JSON_Data) > 0 Then
      var =     "function do_json()" : @CRLF
      var = var:"{" : @CRLF
      var = var:   "var myJSONObject = {""x"": " : JSON_Data : " };" : @CRLF
      var = var:   "myJSONObject.x." : JSON_Return : " = ""hello"";" : @CRLF
      var = var:   "var txt = myJSONObject;" : @CRLF
      var = var:   "return txt;" : @CRLF
;     var = var:   "return JSON.stringify(myJSONObject); " : @CRLF
;     var = var:   "return myJSONObject.x." : JSON_Return : ";" : @CRLF
      var = var:'}':@CRLF
      oScript=CreateObject("MSScriptControl.ScriptControl")
      oScript.language = "javascript"
      oScript.addcode(var)
      Ret = oScript.Eval("do_json()")
      oScript=0
      Return Ret
   Else
      Return "*ERROR*"
   EndIf

#EndFunction

Title: Re: JSON Text from Object
Post by: Deana on June 20, 2014, 01:34:08 PM
Sorry I am not aware of any way to convert that object to text using that method. 

Have you considered using dotNet to handle the JSON?

System.Runtime.Serialization.Json Namespace (only available in .NET 4.5): http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json(v=vs.110).aspx
Json.NET is a popular high-performance JSON framework for .NET http://json.codeplex.com/
Title: Re: JSON Text from Object
Post by: JTaylor on June 20, 2014, 01:45:57 PM
Was looking at that but was hoping to keep it simple, if possible.   Seems odd that there isn't a simple command to do such a thing...guess there is but no idea how to initialize the proper object.  stringify is clearly documented on MSDN but never an example that shows how the JSON object is initialized.

Jim
Title: Re: JSON Text from Object
Post by: stanl on June 21, 2014, 11:52:05 AM
Quote from: JTaylor on June 20, 2014, 01:45:57 PM
Was looking at that but was hoping to keep it simple, if possible.

Not that it helps, but it probably won't be simple. I was jazzed about JSON 4 years ago when with DISH, contributed a few threads, most of them with the script control. Then for higher-impact JSON I was contect with Powershell's JSON cmdlets (called through the CLR).

Last 2 forays I had with JSON failed, and I think because it is not really a standardized system that a single parser can handle.

Good luck.
Title: Re: JSON Text from Object
Post by: JTaylor on June 21, 2014, 12:58:42 PM
Thanks.   Just went a different route at the moment to get the job done.   Maybe someone will chime in with an idea on how to make the JSON object, the docs reference, available so I have access to all those methods.

Jim
Title: Re: JSON Text from Object
Post by: stanl on June 24, 2014, 11:40:45 AM
This looks interesting

http://mono.servicestack.net/mythz_blog/?p=344 (http://mono.servicestack.net/mythz_blog/?p=344)

Title: Re: JSON Text from Object
Post by: JTaylor on June 24, 2014, 05:52:55 PM
Thanks.  Looks interesting.

Jim
Title: Re: JSON Text from Object
Post by: stanl on June 28, 2014, 06:30:12 AM
Quote from: JTaylor on June 24, 2014, 05:52:55 PM
Thanks.  Looks interesting.

Jim

Yeah, and in the user posts in the thread is a reference to FastJSON, which can be compiled into a dll with Studio. Might make a nice addition to using the CLR. I have no immediate need for a serializer at work, but it does sound like an interesting project...
Title: Re: JSON Text from Object
Post by: JTaylor on June 28, 2014, 11:40:21 AM
Thanks again.   My goal is a solution that doesn't require additional pieces to the puzzle but always good to have more than one tool in the toolbox.

I am still extremely puzzled as to how I can turn the JSON text into an object and change/interrogate values with no problem but no apparent way to retrieve the full text of the object.  I have to be missing something simple.

Jim