Json formating output

Post

Posted
Rating:
#1 (In Topic #201)
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 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
                    }
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Can you upload the JSON file please.
Online now: Yes Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
this is my original file :

Code (gambas)

  1. \{
  2.     "data": \{
  3.         "autopilot": \{
  4.             "aircraftType": 0,
  5.             "altHoldRate": 50
  6.             }
  7.        }
  8. }

then I run this program :

Code (gambas)

  1. Public Sub Form_Open()
  2. Dim cReciverDta As Collection
  3.  
  4. cReciverDta = JSON.Decode(File.Load("~/airplane files/testfile.txt"))
  5.  
  6. File.save(("~/airplane files/testfile2.txt"), JSON.Encode(cReciverDta))
  7.  

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
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
Have a look at the attached program. Hopefully this will help.

<IMG src="http://www.cogier.com/gambas/JSON_002.png"> </IMG>

Attachment
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
Try changing the line in my program from: -

Code (gambas)

  1. Dim sFile As String[] = Split(File.Load("../json.txt"), gb.NewLine)

To: -

Code (gambas)

  1. Dim sFile As String[] = Split(File.Load("~/airplane files/testfile.txt"), gb.NewLine)
Online now: Yes Back to the top

Post

Posted
Rating:
#7
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
Thanks that works

I just wish I knew enough to understand the two lines  …. but I will keep learning  :D
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Code (gambas)

  1. Public Sub Form_Open()
  2. Dim sFile As String[]                                     'To store an array
  3. Dim sLoad As String                                       'To store a string
  4.  
  5. sLoad = File.Load("~/airplane files/testfile.txt")        'Load the file, ~/airplane files/testfile.txt, into the string sLoad
  6.  
  7. Print sLoad                                               'Print the file, just so that you can see what's happening
  8.  
  9. sFile = Split(sLoad, gb.NewLine)                          'Take the file(sLoad) and Split each line and put it in the array sFile
  10.  
  11. Stop                                                      'Stop the program
  12.  
  13.  

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.

<IMG src="http://www.cogier.com/gambas/Breakdown1.png"> </IMG>

Attachment

I hope that helps.
Online now: Yes Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
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)
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Guru
cogier is in the usergroup ‘Guru’
Your code works but you can use .Max instead of .Count -1 and you don't need so many brackets in the 'Save' line.

Code (gambas)

  1. For iCount = 0 To sFile.Max
  2.   sLoad2 &= sFile[iCount] & gb.NewLine
  3. File.save("~/testfile2.txt", sLoad2)
  4.  

However Gambas can do this in one line with 'Join', no need for the loop above.

Code (gambas)

  1. File.save("~/testfile2.txt", sFile.Join(gb.newline))

16k!  :o  What luxury!!
I started with the Sincair ZX81 (1981) which had 1k
Online now: Yes Back to the top

Post

Posted
Rating:
#11
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 Thank you, that is what I was looking for.
I wish there was a good book to learn all that from.

My first programming was with an HP 67 … not sure how much memory that had, but could do a lot with it.
Online now: No Back to the top
1 guest and 0 members have just viewed this.