Json formating output

Post your Gambas programming questions here.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Json formating output

Post by grayghost4 »

how do I output a file using Json with line feeds and tabs ?

this is what I get now:
{"data":{"autopilot":{"aircraftType":0,"altHoldRate":10,"attitude":{"0":{"0":{"envelopeNeg":0,"envelopePos":0,"feature":128},"1":{"envelopeNeg":0,"envelopePos":0,"feature":128}},"1":{"0":{"envelopeNeg":0,"envelopePos":0,"feature":128},"1"

what I would like is this:

{
"data": {
"autopilot": {
"aircraftType": 0,
"altHoldRate": 50,
"attitude": {
"0": {
"0": {
"envelopeNeg": 0,
"envelopePos": 0,
"feature": 128
},
"1": {
"envelopeNeg": 0,
"envelopePos": 0,
"feature": 128
}
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Json formating output

Post by cogier »

Can you upload the JSON file please.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Json formating output

Post by grayghost4 »

this is my original file :
{
    "data": {
        "autopilot": {
            "aircraftType": 0,
            "altHoldRate": 50
            }
       }
}
then I run this program :
Public Sub Form_Open()
Dim cReciverDta As Collection

cReciverDta = JSON.Decode(File.Load("~/airplane files/testfile.txt"))

File.save(("~/airplane files/testfile2.txt"), JSON.Encode(cReciverDta))

End
And the optput I get is this :

{"data":{"autopilot":{"aircraftType":0,"altHoldRate":50}}}

How can I format JSON to get the first format?
So either the File.Load or the File.save have remove linefeeds and spaces, or is it the use of a Collection variable
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Json formating output

Post by cogier »

Have a look at the attached program. Hopefully this will help.

Image
Test2-0.0.1.tar.gz
(32.89 KiB) Downloaded 505 times
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Json formating output

Post by grayghost4 »

Thank you for your reply
All I get from the download is the same thing you have in your post and a list of clients.
I must be doing something wrong with the download.

I think the use of the Collection variable is what is removing the format from the data.
so I will have to use a string or string array to modify the data before I write it back to the file.

If json had Prety Print, I think that would solve the problem.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Json formating output

Post by cogier »

Try changing the line in my program from: -
Dim sFile As String[] = Split(File.Load("../json.txt"), gb.NewLine)
To: -
Dim sFile As String[] = Split(File.Load("~/airplane files/testfile.txt"), gb.NewLine)
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Json formating output

Post by grayghost4 »

Thanks that works

I just wish I knew enough to understand the two lines .... but I will keep learning :D
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Json formating output

Post by cogier »

Let's try and breakdown the line Dim sFile As String[] = Split(File.Load("~/airplane files/testfile.txt"), gb.NewLine) for you by separating out the individual commands. The attached program does the same as the single line. I have added a Variable along with a Print and Stop command.
Public Sub Form_Open() 
Dim sFile As String[]                                     'To store an array
Dim sLoad As String                                       'To store a string

sLoad = File.Load("~/airplane files/testfile.txt")        'Load the file, ~/airplane files/testfile.txt, into the string sLoad

Print sLoad                                               'Print the file, just so that you can see what's happening

sFile = Split(sLoad, gb.NewLine)                          'Take the file(sLoad) and Split each line and put it in the array sFile

Stop                                                      'Stop the program 

End
If you run the attached program it will stop at "Stop". When it does use the mouse to highlight sFile and you will see what is in the sFile array.

Image
NewTest.tar.gz
(11.84 KiB) Downloaded 421 times
I hope that helps.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Json formating output

Post by grayghost4 »

Thank you very much, that is a big help.

My last programming was about 40 years ago on an Apple II with integer basic then some UCSD Pascal.
Now I want to get back to programming again, and Gambas looks like it is the best one for me.
Sure is nice to have more than 16 K memory and a Tape drive to save to. :lol:

One more question, after I make changes to the array, what is the easiest way to convert it back to a file?
I came up with this .....

For iCount = 0 to sFile.count -1
sLoad2 &= sFile[iCount] & gb.NewLine
Next
File.save(("~/testfile2.txt"), sLoad2)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Json formating output

Post by cogier »

Your code works but you can use .Max instead of .Count -1 and you don't need so many brackets in the 'Save' line.
For iCount = 0 To sFile.Max
  sLoad2 &= sFile[iCount] & gb.NewLine
Next
File.save("~/testfile2.txt", sLoad2)
However Gambas can do this in one line with 'Join', no need for the loop above.
File.save("~/testfile2.txt", sFile.Join(gb.newline))
16k! :o What luxury!!
I started with the Sincair ZX81 (1981) which had 1k
Post Reply