How to use JScript's JSON parser?

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

Locked
User avatar
Moonchild
Posts: 34
Joined: Mon Apr 04, 2005 12:01 pm

How to use JScript's JSON parser?

Post by Moonchild »

In viewtopic.php?f=5&t=21039 the following was suggested:
Gaev wrote:2) Instead of asking for NeoBook commands to parse JSON object's key/value pairs, you can use Javascript's built in JSON parsing capabilities to extract the individual key/value pairs as NeoBook variables.
How would one go about doing this?
I have data formatted in JSON (on disk) that I need to read and parse in Neobook. Since I've only used NeoBook functions in JScript as simple one-to-one functions so far, I'd like to know how to pass complete resulting arrays back to Neobook that are parsed from the JSON file.

Specifically, it's a file with a list of credentials encoded in JSON, each entry having multiple fields. How would I parse the entire thing and then pass back an array?

Example:

Code: Select all

{
  "nextId":15,
  "logins":
    [
     {"id":13, (unique ID)
      "hostname":"url.to.host",
      "httpRealm":"realm name",
      "formSubmitURL":null,
      "usernameField":"name-of-field",
      "passwordField":"name-of-field",
      "encryptedUsername":"===base64-encoded===",
      "encryptedPassword":"===base64-encoded===",
      "guid":"{00000000-0000-0000-0000-000000000000}",
      "encType":1,
      "timeCreated":1431036705891,
      "timeLastUsed":1431036705891,
      "timePasswordChanged":1431036805297,
      "timesUsed":1
    },
     {"id":14,
      "hostname":"url.to.host",
      "httpRealm":"realm name",
      "formSubmitURL":null,
      "usernameField":"name-of-field",
      "passwordField":"name-of-field",
      "encryptedUsername":"===base64-encoded===",
      "encryptedPassword":"===base64-encoded===",
      "guid":"{00000000-0000-0000-0000-000000000000}",
      "encType":1,
      "timeCreated":1431036705892,
      "timeLastUsed":1431036705892,
      "timePasswordChanged":1431036805299,
      "timesUsed":4
    }
  ],
  "disabledHosts":
  [
    "http://url1.domain1.com",
    "http://url2.domain2.com"
  ],
  "version":1
}
I'd like to get back an array of "logins" and of "disabledHosts", but I'm unsure how to pass back arrays or pull the data out of the JSON data format. The rest is rather inconsequential and doesn't need to be retained.
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: How to use JScript's JSON parser?

Post by Gaev »

Moonchild:
I have data formatted in JSON (on disk) that I need to read and parse in Neobook.
1) Having data in a text file with { } wrapped around them is not the same as a Javascript Object (variable) whose key/value pairs are referenced using the syntax known as Javascript Object Notation (JSON).

2) InternetGet/InternetPost commands work with the http: protocol ... normally, you would have some kind of server script (like php or java or ruby) return the data as an object ... if your file is on local disk, it would be using the file: protocol

There might be a way to FileRead your local text file, pass it to an HTML document running in the WebBrowser widget, and have it return the parsed data as NeoBook variables (array) ... but before I research this, let me know where your file is located ... and confirm that you can view it using a text editor like notepad.exe
Since I've only used NeoBook functions in JScript as simple one-to-one functions so far, I'd like to know how to pass complete resulting arrays back to Neobook that are parsed from the JSON file.
Specifically, it's a file with a list of credentials encoded in JSON, each entry having multiple fields. How would I parse the entire thing and then pass back an array?
How would one go about doing this?
The structure of data presented in your post is a (complex) "nested object" i.e. the key Logins has a value that is a Javascript Array ... and each item in the array is itself an object with its own key./value pairs ... I don't think you can pass the entire thing back as a NeoBook Array Variable (which does a good job of simulating one dimensional arrays) ... you probably need to have it pass back the array ... then repeatedly pass each Array text back to have it parsed into keys and values ... something you would do in a javascript client code too.
User avatar
Moonchild
Posts: 34
Joined: Mon Apr 04, 2005 12:01 pm

Re: How to use JScript's JSON parser?

Post by Moonchild »

I'm sorry but what I pasted was a direct copy from a valid JSON file... It doesn't require an http request as it's a data storage/exchange format, regardless of how the data is acquired. You seem to assume that I don't know what it is and getting on my case about it. That's not a very nice way to respond to a request for help.

If I can't do this through a json.parse() and parsing the object elements because passing the data back to NB is a mess (as it seems to be) and arrays aren't really supported, then I'll have to write my own JSON parser in NB script, I guess. Unless someone has a better idea?
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: How to use JScript's JSON parser?

Post by Gaev »

Moonchild:
You seem to assume that I don't know what it is and getting on my case about it. That's not a very nice way to respond to a request for help.
I did not assume anything about your knowledge (as I don't know you from Adam) ... going from the way your post was constructed, I assumed that you had a text file on local disk whose content was as posted.
I'm sorry but what I pasted was a direct copy from a valid JSON file... It doesn't require an http request as it's a data storage/exchange format, regardless of how the data is acquired.
I am not sure I understand this part ... but for fear of upsetting you, I will refrain from trying to assist you any further ... hope someone else can be of more assistance.
User avatar
Moonchild
Posts: 34
Joined: Mon Apr 04, 2005 12:01 pm

Re: How to use JScript's JSON parser?

Post by Moonchild »

Long story short: JSON, like XML, is a data storage/transmission format in the broadest sense of the word. Yes, in my case it is data stored in a local file, and no, it doesn't require server-client communication (although that is the most common use of the format), nor does it need an Internetget/post or http protocol or web browser object to deal with.

Considering NB has JScript available, I wanted to use json.parse() in it, and returning the data I want (after pulling it out of the resulting javascript object) to the NB application. Alternatively, vbscript could also be used since I have an implemented JSON parser in vbs already as well.
But if I can't easily pass arrays back to NB from defined functions (in the function library, where I would create this), which is my main concern here, then it quickly becomes difficult to do, and I'll have to parse it in NB natively in its actionscript, instead, which I hoped to avoid since it's not particularly efficient in handling larger amounts of data. Individually defining separate variables for each element of each array entry is.. well, not going to work very well.
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: How to use JScript's JSON parser?

Post by dpayer »

Moonchild wrote: But if I can't easily pass arrays back to NB from defined functions (in the function library, where I would create this), which is my main concern here, then it quickly becomes difficult to do, and I'll have to parse it in NB natively in its actionscript, instead, which I hoped to avoid since it's not particularly efficient in handling larger amounts of data. Individually defining separate variables for each element of each array entry is.. well, not going to work very well.
It appears no one has generated this type of NB function yet but I'm sure the community would be benefited from your contribution of such a function to the resource section of this site.

Regarding "defining separate variables for each element of each array", I would think this is a great place to use nested variables [var[x]] or [[var][xx]] which would allow you to couple the name of the file with the individual variable name to make unique NB variables.

I look forward to hearing your solution to this issue.
David Payer
Des Moines, Iowa
USA
Ferdari
Posts: 32
Joined: Sun Sep 13, 2009 9:34 pm
Contact:

Re: How to use JScript's JSON parser?

Post by Ferdari »

Did you find a solution parsing JSON? and getting into neobook variables, im having really bad time and too much headaches trying to parse data, i created regex but its not a real parser and im getting errors with some data,

also its dissapointing not having codification functions, my JSON comes in UTF8, but neobook cant handle this data. :evil:
http://www.noten.in
We have Apps!
Tony Kroos
Posts: 419
Joined: Thu Oct 15, 2009 3:43 pm

Re: How to use JScript's JSON parser?

Post by Tony Kroos »

Post your json example
Locked