2

I have an item called result, where result is a .JSON object:

    {
         "id": "1345",
         "day": "Tuesday",
         "title": "Economics210"

     }

Is there a way to save this to a .json file in Javascript without var fs = require('fs') (Node)? This is all I see on the S.O. questions so far.

Perhaps by converting to text, then .json? Any ideas?

haxtar
  • 1,962
  • 3
  • 20
  • 44
  • 1
    Are you trying to accomplish this without file io? Copy paste is your only option. Or are you trying to achieve this in the browser? – Dave May 01 '18 at 01:23
  • JSON is just a text format. It can be written by any language or by hand. – Cfreak May 01 '18 at 01:24
  • @Dave file io is fine just so long as I don’t have to use node I guess? – haxtar May 01 '18 at 01:34
  • 2
    hey there haxtar — we're just missing some context here — there's a practically unlimited number of ways to save a file using a computer — considering we know you cannot use `node`, we need to know what other means are available to you in whatever environment you're operating under — if you're writing a python script for example, you should save the text file with the python standard library for file io — if you're writing a script for a web browser, you might want to use ajax to save to a web server, or perhaps use `localStorage` to write to the user's disk – ChaseMoskal May 01 '18 at 01:41
  • @ChaseMoskal so the json file and html file in which the JS script is being written are both hosted on the same server, in the same location. I just want a way to modify the json file in the location it’s in by basically overwriting it to include the contents of result. Hope that was clearer – haxtar May 01 '18 at 01:49
  • 1
    @haxtar — okay, you are writing a browser script — browser pages are unable to directly interact with the server's filesystem — you are left with two primary options: (1) add functionality to the web server which can save files on behalf of the webpage (2) your page can save/load directly from `localStorage` — i'll post an answer to explain – ChaseMoskal May 01 '18 at 01:56
  • Possible duplicate of [Create a file in memory for user to download, not through server](https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server) – Jared Smith May 01 '18 at 02:21
  • @ChaseMoskal thanks for your comments, I appreciate you taking the time to clear things up – haxtar May 01 '18 at 02:31

2 Answers2

5

to save a file from browser-based javascript, you've got three basic directions to go:

  1. add server functionality to save files

    • you have to know how to add functionality to your web server
    • you have to implement ajax in your browser page, which would make a POST request to the server, asking to save the file
    • the server would then save the file
    • you can set up a web server to do this with node, python, and about a billion other programming languages and platforms
    • the benefit to this method, is that the server is truly updating the file for everyone to see the update
  2. use localStorage on the webpage

    • localStorage is a way for webpages to save data to their users' disk
    • localStorage data is not shared between users — a user can only interact with their own localStorage data
    • you can save and load entire JSON files in localStorage
  3. allow the user to download the file from their browser

ChaseMoskal
  • 7,151
  • 5
  • 37
  • 50
  • This does not answer the question. If you are going to say that the question itself is in some way "wrong" (and I wouldn't argue with you if you did) then explain why to the OP with your proposed alternative, not just offering it in lieu of answering the actual question. – Jared Smith May 01 '18 at 02:25
  • My question wasn’t wrong because the premise of it was based on something I wasn’t even knowledgeable about, which @chasemoskal cleared up, actually. – haxtar May 01 '18 at 02:28
  • Thanks @ChaseMoskal, I’m going to check this out right when I get the chance – haxtar May 01 '18 at 02:28
  • added third possibility mentioned by @JaredSmith – ChaseMoskal May 01 '18 at 03:15
2

Replace "File content" with JSON.stringify(yourObj) and you will be able to download it.

You can read more there: How to create a file and generate a download with Javascript in the Browser (without a server)

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

// Start file download.
download("hello.json","File content");
Profesor08
  • 1,181
  • 1
  • 13
  • 20