JSONItem.ToString

Hi there,

I have a question about converting JSONItems to strings.

If I do this:

[code]dim j as new JSONItem
dim j2 as new JSONItem
j.value( “One” ) = “One”
j.value( “Two” ) = “Two”
j.value( “Three” ) = “Three”
j.value( “Four” ) = “Four”
j2.value( “Child 1” ) = “Child 1”
j2.value( “Child 2” ) = “Child 2”
j.value( “ChildItem” ) = j2

msgBox(j.toString)
[/code]

will j.toString always contain the elements in the same order as I added them, with the same sorting etc?

I’m wanting to save a bunch of stuff from a database to a JSON entry via a method, leave it for a while, then compare if the existing state of the record in the database matches the saved state. I can’t do it with database flags or anything like that; I have to use a saved version of the data.
A really nice simple way of doing it would be to make a method to save the info I’m looking at as a JSON string, then at a later date compare that JSON string with the JSON string of the current state of the record, but obviously that won’t work if the items aren’t in the same order.

I wonder if it’s the same as the behaviour of For… Each:

(i.e. “It’s ok now, but can’t guarantee for the future”).

I really don’t want to have to go through every entry in the JSON, child by child!

compute an MD5 for the entry in the DB and compare the computed MD5’s instead ?

depending on the DB you might be able to do this all in the DB

or do you need to know not just that it did change but what changed ?

I need to know what’s changed, but only of a specific subset of fields, from one parent table, and a number of child tables.
We may also add fields to the DB in future, so I’m not sure an MD5 will work. We need the JSONItem for other things, hence trying to do it this way!

Hamish you already might know this and/or might not pertain, but the order of JSON isn’t guaranteed either. I ran into this and Joe R. helped me realize this as the JSON spec doesn’t guarantee order.

Bah. (Not to you. To JSON. And not to Jason. He’s different.)

I’d save a table that is a “change log” so you can just query that table to see if the record in question has entries after any given date, what field changed, who changed it, etc

you can make this as sophisticated as you want / need

That’s what I would love to do, but for Boring Technical Reasons ™ it’s going to be really complicated to do it that way. As the JSON approach won’t work I’ll simply read the values I need and compare them.

Could you run the values into an in memory SQLite table and then you have the full power of SQL to test what has changed?

Sorry for the slow reply here.

I think I’ve explained myself badly before, so sorry about that. Also, I think that my requirements have changed as I’ve worked through the code!

The end result I came up with was that I just needed to find out if things had changed, but not what had changed. I now have a method which converts a row from a table (plus some child rows from other tables) into a JSONItem; while doing so, it also creates a long string from the values and, at the end of the method computes an MD5 hash of that string. The JSONItem has the hash stored with it.
When I want to do my comparison, I ask the same method for the JSONItem for the row, and compare the MD5 values from the saved one and the current one. If different, I know they’ve changed, and do what I need to do.

This way, I can change the database structure if I need to (adding columns to my original table) and in my JSON-creation method I can control which fields are used, so even if the structure changes, the MD5 will remain the same for a given row, unless the data within it changes.

Cheers for the help, all.