Page 1 of 1

How to use JScript's JSON parser?

PostPosted: Sun May 17, 2015 5:26 am
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.

Re: How to use JScript's JSON parser?

PostPosted: Sun May 17, 2015 6:23 am
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.

Re: How to use JScript's JSON parser?

PostPosted: Sun May 17, 2015 2:11 pm
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?

Re: How to use JScript's JSON parser?

PostPosted: Sun May 17, 2015 5:55 pm
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.

Re: How to use JScript's JSON parser?

PostPosted: Mon May 18, 2015 1:25 am
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.

Re: How to use JScript's JSON parser?

PostPosted: Mon May 18, 2015 7:06 am
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.

Re: How to use JScript's JSON parser?

PostPosted: Sun Dec 27, 2015 11:33 pm
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:

Re: How to use JScript's JSON parser?

PostPosted: Mon Dec 28, 2015 8:41 am
by Tony Kroos
Post your json example