Backup Realtime Database to a .json file and compare it with the file size from the previous backup

Hello everyone!
Is it possible to automate the process of saving a .json-file from an app to compare its size with the previously saved file?
For me, the main thing is to get the URL for the file save command (see image) and the extension that compares two files by size. I don't see any problems beyond that.

You can’t automate the “Export JSON” button from the Firebase console. That option is only manual and doesn’t have any API behind it.

Instead, you should use Firebase’s REST API. You can download your database as JSON by calling:
https://your-db-name.firebaseio.com/.json

In App Inventor, just use the Web component to make a GET request to that URL and then save the response as a JSON file.

For comparing backups, App Inventor doesn’t easily give file size, so the simplest way is to compare the length of the JSON response. If the length changes, your data has changed. A better option (if possible) is to generate a hash (like MD5 or SHA) and compare that instead.

Also, if your database is secured, you’ll need to add your auth token to the URL.

So basically: don’t try to automate the export button—use the REST API and compare the data yourself.

2 Likes

If you must check file size, and can use extensions:

FileSizeV2

1 Like

Please tell me how?

You can do it like this in App Inventor:

  1. Add a Web component and a File component
  2. Set the Web URL to:
    https://your-db-name.firebaseio.com/.json
  3. Call:
    Web1.Get
  4. When the response comes (Web1.GotText event):
  • Take the responseContent
  • Save it using File component like:
    • File name: backup.json
    • Content: responseContent

So flow is:

  • Web.Get → GotText → File.Save

That will create your JSON backup automatically.

If your database is secured, just add:
?auth=YOUR_SECRET
to the URL.

Then for comparison:

  • Either compare the length of responseContent
  • Or use an extension (like FileSize) to compare file sizes
2 Likes

Thank you all for your help. You are the best!

The structure of .json doesn't work, only in text form.

Then parse it back to a JSON with the JsonTextDecode block from the web component

image

1 Like

Apparently I'm doing something wrong...

Ah, you want it pretty printed!

Unfortunately, it seems that AppInventor minifies the json on import (responseContent) and there is no built-in method to pretty print the json to file.

Viewing pretty printed can be done in a webviewer with some javascript:

https://www.geeksforgeeks.org/javascript/how-to-pretty-print-json-string-in-javascript/

I just doubt there won't be any issues restoring this backup in Firebase—I could try, in principle...- I tried it, but it doesn't work.

Also this:

I may have a look at creating an extension to save pretty printed json to file, when I find the time.

1 Like

Thank you for your work. I'll be waiting.

Well that didn't take long:

Pretty Print JSON

2 Likes

Great job! Thank you.

Always returns "0"

Try removing file://

No - it didn't work

The extension is working (and yes it also works if the file extension is .json):

from my previous example where the json file is saved to the ASD. It is possible that the extension does not have access permission to the file you are saving.

I would suggest working in the ASD, then copying the file out from there if you need it in shared storage.

1 Like

Okay - I'll give that a try.