Am i doing something wrong with my attempts to create simple json from simple queue?
This is my testing procedure to manually create json file from queue:
MAKE_JSON PROCEDURE
qJSONS QUEUE,PRE(QJ)
Name STRING(30)
Surname STRING(30)
Address STRING(60)
END
ToJson JSONClass
Collection &JSONClass
I Long
CODE
I = 0
free(qJSONS)
QJ:Name = 'ROBERT'
QJ:Surname = 'DUVALL'
QJ:Address = 'CANADA'
add(qJSONS)
QJ:Name = 'TOM'
QJ:Surname = 'CRUISE'
QJ:Address = 'IN MISSION'
add(qJSONS)
QJ:Name = 'BARNEY'
QJ:Surname = 'FLINSTONE'
QJ:Address = 'FLINSTON'
add(qJSONS)
ToJson.Start()
GET(qJSONS,1)
LOOP I = 1 TO RECORDS(qJSONS)
GET(qJSONS,I)
COLLECTION &= ToJson.CreateCollection('PERSON')
COLLECTION.APPEND('NAME',QJ:Name)
COLLECTION.APPEND('SURNAME',QJ:Surname)
COLLECTION.APPEND('ADRESA',QJ:Address)
END
ToJson.SaveFile('test.JSON',1,0)
But I always got just one record in the created json, and just only last record from queue…
I know for simple saving entire queue structure with: ToJson.Save(qJSONS,'test.json'), but I need it to do manually…
I assume you’re using jFiles here? You’re massivly over thinking this. Paste your json into capesoft.com/jfilecode and it’ll write the correct jfiles code for you. It should be about 3 lines long to export the queue.
I don’t use jFiles but I would guess COLLECTION &= ToJson.CreateCollection('PERSON') in the LOOP is wrong as it will dispose all the prior Append() lines leaving just the last. It should be moved before the LOOP:
ToJson.Start()
COLLECTION &= ToJson.CreateCollection('PERSON') !<-- Moved Before Loop
GET(qJSONS,1)
LOOP I = 1 TO RECORDS(qJSONS)
GET(qJSONS,I)
!moved before loop--> COLLECTION &= ToJson.CreateCollection('PERSON')
COLLECTION.APPEND('NAME',QJ:Name)
COLLECTION.APPEND('SURNAME',QJ:Surname)
COLLECTION.APPEND('ADRESA',QJ:Address)
END
st StringTheory
people Queue,Name('people')
name STRING(255),Name('name')
surname STRING(255),Name('surname')
address STRING(255),Name('address')
End
json jsonClass
CODE
!-- Save From Structure into StringTheory object ---
json.start()
json.SetTagCase(jf:CaseAsIs)
json.SetDontSaveBlankArrayValues(true)
json.Save(people,st,'people') ! Save to a StringTheory object
st.SaveFile('test.JSON')
And to Load into the same queue:
st.LoadFile('test.JSON')
json.start()
json.SetTagCase(jf:CaseAsIs)
json.Load(people,st,'people') ! Load From a StringTheory object