Write to JSON file

I’m using NodeJS and need to create and write to a json file on the server from the backend. Is there an extension or a native Wappler way for that?

I only found an extension for php.

If not then I’ll go ahead and write a custom module. I just want to make sure I didn’t miss any existing option.

Thanks!

1 Like

Don’t think I have seen anyone release a custom module to write files in the community.
So you would have make your own.

1 Like

Thanks for confirming. I’ll go the custom module route and release it here for others when done. Native node stuff so no npm packages involved.

Natively it’s something like

const mw = require('mw')
const mything = {{myWapplerObject}}
}
const jsonString = JSON.stringify(mything )fs.writeFile('./newFile.json', jsonString
})
1 Like

Looks good. :slight_smile:

It would be more helpful for beginners if you create a separate post with HSJON & JS files in the Wappler Extension category.

1 Like

I haven’t been bothered to write a full extension yet as my use cases are very simple and I just resort to write a few lines and use RunJS extension but I agree it would be useful if you could release an extension for this.

1 Like

I’ll write a custom module and then post the GitHub repository in the extension channel which will include the HSJSON and JS files. I’m in the middle of writing two other custom modules as well. One for changing file permissions and one for running ssh commands on a remote server. So far it’s going well.

1 Like

hi @tbvgl ,
If you’re using mysql, something like this might work for you.
You can check the JSON_OBJECT and JSON_ARRAYAGG functions in mysql.

Example 1 :

SELECT JSON_OBJECT
    ('address_id', address_id,
     'address', address, 
     'address2', address2)
      FROM location INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/location.json'

After the file is created here, it will be enough to move the file to the folder you want with the wappler functions.
PS : The example I have shown is a local example, you must specify a suitable path to your own server.

image

Example 2 :

SELECT 
    CONCAT("[",
         GROUP_CONCAT(
              CONCAT("{address_id:'",address_id,"'"),
              CONCAT("address:'",address,"'"),
              CONCAT(",address2:'",address2,"'}")
         )
    ,"]") 
AS json FROM location;

Output :

[{address_id:’1’address:’1586 Guaruj Place’,address2:’47 MySakila Drive’},{address_id:’3’address:’360 Toulouse Parkway’,address2:’270, Toulon Boulevard’}]

Thanks for sharing that @s.alpaslan!

It won’t work for my case because I need a more flexible solution that allows formatting the content after getting a part of it from the db. Your use case will work for other people though.

I’m done with the wappler extension for writing files. I’ll test the error reporting a little more and then post the repo in the extension channel in the coming days. The extension will allow you to write any text content to any file format in any folder that you create before.

2 Likes

Looking forward this one! Feel free to reach out to seek feedback

Here is the write/append/create file extension: NodeJS Extension to Create/Write/Append Files

@Apple the ssh extension will be the next one. I’ll keep you posted.

I’ve seen alaSql recently, you can try this, maybe it will work for you.
http://alasql.org/

Thanks but I’ve solved my problem already with the extension I posted above :slight_smile:

1 Like