viewpoint-particle

Author Topic: JSON Text from Object  (Read 5674 times)

JTaylor

  • Pundit
  • *****
  • Posts: 1014
    • Data & Stuff Inc.
JSON Text from Object
« 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

#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

 

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: JSON Text from Object
« Reply #1 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/
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

  • Pundit
  • *****
  • Posts: 1014
    • Data & Stuff Inc.
Re: JSON Text from Object
« Reply #2 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

stanl

  • Pundit
  • *****
  • Posts: 935
Re: JSON Text from Object
« Reply #3 on: June 21, 2014, 11:52:05 am »
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.

JTaylor

  • Pundit
  • *****
  • Posts: 1014
    • Data & Stuff Inc.
Re: JSON Text from Object
« Reply #4 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

stanl

  • Pundit
  • *****
  • Posts: 935
Re: JSON Text from Object
« Reply #5 on: June 24, 2014, 11:40:45 am »

JTaylor

  • Pundit
  • *****
  • Posts: 1014
    • Data & Stuff Inc.
Re: JSON Text from Object
« Reply #6 on: June 24, 2014, 05:52:55 pm »
Thanks.  Looks interesting.

Jim

stanl

  • Pundit
  • *****
  • Posts: 935
Re: JSON Text from Object
« Reply #7 on: June 28, 2014, 06:30:12 am »
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...

JTaylor

  • Pundit
  • *****
  • Posts: 1014
    • Data & Stuff Inc.
Re: JSON Text from Object
« Reply #8 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