[Desktop] Export to JSON

Hi,

I may be wrong in wanting to do that that way, but…

In my export to XML, I generate files like this one:

<?xml version="1.0" encoding="UTF-8"?> <root> <Header> <PNom>Prénom</PNom> etc. </Header> <RecordSet> <Record_0 Name="Monsieur Dien Van Bolt"> <PNom>Dien</PNom> etc.

<Record_0 Name= Monsieur Dien Van Bolt is build using the PNom, Nom, Sexe … Columns. I can do the same for JSON, but I do not found how to create <Record_0 Name=…

Converted to JSON, this would looks like:

json code [ {"Header":"11 Columns"}, {"PNom":"Prénom"}, // Etc. } {"Record_0":"Monsieur Dien Van Bolt"}, {"PNom":"Dien"}, {"Nom":"Van Bolt"}, // etc. } ]

Ideas ?

JSON is a simpler, more limited format, so think of it in terms of arrays and (in Xojo) dictionaries. Considerder a format more like this:

{
  "Columns" : [
    {"PNom" : "Prnom"},
    { ... }, ...
  ],
  "Records" : [
  ...
  ]
}

Thanks Kem.

I suspected that, but I could not believe it can be that. Now I know.

The best I could do, based on a LR example, was:

[code] Dim Family As New JSONItem

Family.Value(“Description”) = “Information about the Doe Family”
Family.Value(“Parents”) = “John and Jane”
Family.Value(“Childs”) = “Jasond, Jarod, Jessica”

// This object is manipulated like a dictionary
Dim Father As New JSONItem

Father.Value(“Name”) = “John Doe”
Father.Value(“Age”) = 32
Father.Value(“Married”) = True
Father.Value(“Spouse”) = “Jane Doe”

Dim Mother As New JSONItem

Mother.Value(“Name”) = “Jane Doe”
Mother.Value(“Age”) = 30
Mother.Value(“Married”) = True
Mother.Value(“Husband”) = “John Doe”

Dim Kid_First As New JSONItem

Kid_First.Value(“Name”) = “Jasond Doe”
Kid_First.Value(“Age”) = 12
Kid_First.Value(“Father”) = “John Doe”
Kid_First.Value(“Mother”) = “Jane Doe”

Dim Kid_Second As New JSONItem

Kid_Second.Value(“Name”) = “Jarod Doe”
Kid_Second.Value(“Age”) = 10
Kid_Second.Value(“Father”) = “John Doe”
Kid_Second.Value(“Mother”) = “Jane Doe”

Dim Kid_Third As New JSONItem

Kid_Third.Value(“Name”) = “Jessica Doe”
Kid_Third.Value(“Age”) = 8
Kid_Third.Value(“Father”) = “John Doe”
Kid_Third.Value(“Mother”) = “Jane Doe”

dim root as new JSONItem

root.IndentSpacing = 10

root.Append Family
root.Append Father
root.Append Mother
root.Append Kid_First
root.Append Kid_Second
root.Append Kid_Third[/code] *

The ToString result is:

[{"Description":"Information about the Doe Family","Parents":"John and Jane","Childs":"Jasond, Jarod, Jessica"},{"Name":"John Doe","Age":32,"Married":true,"Spouse":"Jane Doe"},{"Name":"Jane Doe","Age":30,"Married":true,"Husband":"John Doe"},{"Name":"Jasond Doe","Age":12,"Father":"John Doe","Mother":"Jane Doe"},{"Name":"Jarod Doe","Age":10,"Father":"John Doe","Mother":"Jane Doe"},{"Name":"Jessica Doe","Age":8,"Father":"John Doe","Mother":"Jane Doe"}]

And I stopped there just because I was searching a way to do what I asked far top.

So, now I have to put the TABLE contents after the code above… (in my real project).

If you are curious to know why I use kids name that starts with a J… John, Jane, seems obvious to me, unconsciously :wink:

Have a look at the Dictionary in the new framework http://developer.xojo.com/xojo-core-dictionary which has this nice little function to GenerateJSON.

Yes, the new framework features are very nice, but also much slower (for now) on anything other than a Mac.

Thanks.

Since I run 2015r1…

On another front, the good new is that JSON deals with Return by itself (it replace EndOfLine with
…).

I think I’m done for the Export to JSON part now. I have to write the Import from JSON to know if my write is correct.

I nealy forgot: the target data base (for the client) have few Records (around 2,000). The speed is not critical here, You are right: always create code with speed in mind.

I’ll also mention my JSONItem_MTC, a drop-in replacement for JSONItem, is faster, has a few more options, and fixes some bugs found in the native version.

https://github.com/ktekinay/jsonitem_mtc

Thanks.

After… 9 hours:

I added two JSONItems one before each “segments” (understand TABLE contents).

For your info, the Xojo code is:

[code] Dim JSON_Head_Seg As New JSONItem

// Store the Name of the segment
JSON_Head_Seg.Value(“TABLE”) = “Heading” // TABLE: Heading

// Store the JSON_Row “object” into JSON_root
JSON_root.Append JSON_Head_Seg[/code]

JSON result:

[ {"TABLE":"Heading"},

In a flat file format, it is still possible to add things, provided the file Reader knows what to do with it.

PS: I can even add (after Heading) ColCnt=11 for example:

Xojo:

JSON_Head_Seg.Value("TABLE") = "Heading ColCnt=11"

JSON result:

[ {"TABLE":"Heading ColCnt=11"},

And this without adding code to the JSON language.