Skip to content
Advertisement

Is it possible to get JSON from a URL and save it as json file with a custom name inside a specified path/folder through the front end?

My issue is simple on paper – I have a React Native project and I’m trying to make a script that will run on build and fetch JSON from a URL, then save it as JSON file with a custom name inside a specified path/folder. For example, I want to access the JSON at www.example.com and save it as a JSON file in project/src/assets/locales/en.json

The problem is that when I google how to fetch and save JSON, most of the results are related to Node.js and I don’t think I can use them. Is what I’m trying to do possible?

Advertisement

Answer

Since you’ve said you’re doing this in a git push hook, you can use any of several things for this:

  1. You can indeed use Node.js, by having the hook run it via node, and then using Node.js’s http client (to read it) and fs module (to write it to a file).

  2. You can use a shell script, perhaps using with curl or wget (on platforms where those are available).

Since you’re already writing JavaScript code for the app itself, Node.js is a reasonable choice.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement