Jump to content

Dynamic JSON arrays


Recommended Posts

I am building a dynamic database in JSON with a JavaScript HTA. I am stuck on "visualizing" how to traverse the array.

The easiest way I can explain this in something everyone knows is an email program's Inbox folders. You start out with an empty folder, all the new messages go in it. Fine.

Now, the user wants to add a sub-folder, Mark, and sub-folder Work. Fine, I can do that. Building it is easy.

Inbox

---Mark (folder)

---Work (folder)

.....messages in Inbox root not in a sub-folder....

Now the user is going crazy:

Inbox

---Mark (folder)

---Work (folder)

------Accounting (folder)

------Shipping (folder)

---------National (folder)

---------International (folder)

This is all fine if I make the JSON array "hardcoded" from the start so I would know how to access it:

Inbox[1].Work[1].Shipping[0].National[0].Message1

BUT, it was created by the user, so how do I step through the dynamically created JSON database not knowing the names (per se) and the depth of the array?

Essentially, I need a JavaScript database that will traverse the array for me looking for the ID (or whatever tag) I give it:

var message=jsonDB.find("ID","f7df68yfudsaf67fd");

or

var message=jsonDB.find("From","eat@joes.com");

That returns an object pointing to that email message, in which I can do:

message.From="eat@joes.com";

message.To="bugs@bunny.com";

The same issue above also carries over to deleting the email message, deleting entire folders, etc.

var message=jsonDB.delete("ID","87hds732h");

I looked at TaffyDB but what I tried didn't work. It doesn't seem to like nested arrays.

Edited by mritter
Link to comment
Share on other sites

  • 3 months later...

I am building a dynamic database in JSON with a JavaScript HTA. I am stuck on "visualizing" how to traverse the array.

The easiest way I can explain this in something everyone knows is an email program's Inbox folders. You start out with an empty folder, all the new messages go in it. Fine.

Now, the user wants to add a sub-folder, Mark, and sub-folder Work. Fine, I can do that. Building it is easy.

Inbox

---Mark (folder)

---Work (folder)

.....messages in Inbox root not in a sub-folder....

Now the user is going crazy:

Inbox

---Mark (folder)

---Work (folder)

------Accounting (folder)

------Shipping (folder)

---------National (folder)

---------International (folder)

This is all fine if I make the JSON array "hardcoded" from the start so I would know how to access it:

Inbox[1].Work[1].Shipping[0].National[0].Message1

BUT, it was created by the user, so how do I step through the dynamically created JSON database not knowing the names (per se) and the depth of the array?

Essentially, I need a JavaScript database that will traverse the array for me looking for the ID (or whatever tag) I give it:

var message=jsonDB.find("ID","f7df68yfudsaf67fd");

or

var message=jsonDB.find("From","eat@joes.com");

That returns an object pointing to that email message, in which I can do:

message.From="eat@joes.com";

message.To="bugs@bunny.com";

The same issue above also carries over to deleting the email message, deleting entire folders, etc.

var message=jsonDB.delete("ID","87hds732h");

I looked at TaffyDB but what I tried didn't work. It doesn't seem to like nested arrays.

Maybe you could try the solution from here, then you would not need to know the propery names:

http://efreedom.com/Question/1-2824299/Select-First-Property-Unknown-Name-First-Item-Array-JSON

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...