Hi there,
I have read through JFiles doc and am still confused with how to create nested Json file. Here is a simple structure:
Buffer QUEUE,PRE(BUF)
ID LONG
Name STRING(30)
END
Images QUEUE,PRE(IMG)
ID LONG
FileName STRING(100)
END
I would like the Images queue to be nested in Buffer, i.e., each record in Buffer queue can have multiple Images records. PS, I have learnt and is able to create single level JSON by: JSon.Save(Buffer, ‘output.json’)
Thanks a lot.
Hi Tony,
I put together an example JSON structure that I believe matches what you’re aiming for:
[
{
"ID": 1,
"Name": "John",
"Images": [
{ "ID": 101, "FileName": "john_photo1.jpg" },
{ "ID": 102, "FileName": "john_photo2.jpg" }
]
},
{
"ID": 2,
"Name": "Alice",
"Images": [
{ "ID": 201, "FileName": "alice_profile.png" },
{ "ID": 202, "FileName": "alice_event.jpg" }
]
}
]
I then used the jFilesCode tool to generate the structure code, and added some sample data to the queues based on what it provided.
PROGRAM
INCLUDE('JFiles.inc')
INCLUDE('StringTheory.inc')
Buffer QUEUE, NAME('Buffer')
ID LONG, NAME('ID')
Name STRING(255), NAME('Name| opt')
Images &ImagesQueueType, NAME('Images| opt | queue')
END
ImagesQueueType QUEUE, TYPE, NAME('Images')
ID LONG, NAME('ID')
FileName STRING(255), NAME('FileName')
END
st StringTheory
json jsonClass
CODE
! --- First Buffer: John ---
Buffer.ID = 1
Buffer.Name = 'John'
Buffer.Images &= NEW(ImagesQueueType)
Buffer.Images.ID = 101
Buffer.Images.FileName = 'john_photo1.jpg'
ADD(Buffer.Images)
Buffer.Images.ID = 102
Buffer.Images.FileName = 'john_photo2.jpg'
ADD(Buffer.Images)
ADD(Buffer)
! --- Second Buffer: Alice ---
Buffer.ID = 2
Buffer.Name = 'Alice'
Buffer.Images &= NEW(ImagesQueueType)
Buffer.Images.ID = 201
Buffer.Images.FileName = 'alice_profile.png'
ADD(Buffer.Images)
Buffer.Images.ID = 202
Buffer.Images.FileName = 'alice_event.jpg'
ADD(Buffer.Images)
ADD(Buffer)
! --- Serialize with JFiles ---
json.start()
json.SetTagCase(jf:CaseAsIs)
json.SetDontSaveBlanks(true)
json.SetDontSaveBlankArrays(true)
json.SetDontSaveBlankArrayValues(true)
json.SetDontSaveBlankGroups(true)
json.Save(Buffer, st)
MESSAGE(st.GetValue(), 'Nested JSON Output')
! --- Optional cleanup ---
json.DisposeQueue(Buffer)
Hopefully this gives you a working starting point!
Mark
2 Likes
Hi Mark,
Thanks for your reply, I think this is quite close but I got some strange results from your code:
[
{
"ID" : 1,
"Name" : "John",
"Images" : "( h\u0000"
},
{
"ID" : 2,
"Name" : "Alice",
"Images" : " h\u0000"
}
]
Any ideas?
I did have a bug I noticed after the post, and fixed as quick as I could, so maybe try again?
Running the above code on my end shows
[
{
"ID" : 1,
"Name" : "John",
"Images" : [
{
"ID" : 101,
"FileName" : "john_photo1.jpg"
},
{
"ID" : 102,
"FileName" : "john_photo2.jpg"
}
]
},
{
"ID" : 2,
"Name" : "Alice",
"Images" : [
{
"ID" : 201,
"FileName" : "alice_profile.png"
},
{
"ID" : 202,
"FileName" : "alice_event.jpg"
}
]
}
]
I am having the same odd result, maybe is because of the template versions? I am using jFiles 1.86 and StringTheory 3.6
Ah, sorry about that —
I was using jFiles 3 for the example, and I’d definitely recommend upgrading if possible.
You might still be able to achieve similar results with jFiles 1, but it’s been so long since I last used that version that I’m afraid I wouldn’t be much help there.
jFiles 3 adds a lot of functionality and really simplifies working with JSON — both reading and writing become much easier.
Mark
Thanks Mark, I will have it a try.