0
I would like to save JSON data to my local pc files.How do I do this with JavaScript,not Node.js?
I want the JSON saved as a file on my pc
11 Antworten
+ 3
You can create a json blob and save it with a simple anchor
+ 3
steve Purpose null and 2 are optional arguments. The second argument (replacer) can be used to customize which elements of the JSON object are included in the result. If it is null, everything is included. The third element is the number of whitespace characters used for indentation.
+ 2
Client side JavaScript has very limited access to the local filesystem for security reason.
https://stackoverflow.com/questions/65066733/how-to-save-json-into-a-local-file-on-my-computer-without-nodejs
Maybe check if the HTML LocalStorage API is suitable for your needs.
https://www.w3schools.com/html/html5_webstorage.asp
+ 2
steve Purpose Yes, an anchor tag... The process is create a blob with the json content, create the url for it and download it with an anchor
See here https://developer.mozilla.org/en-US/docs/Web/API/Blob
+ 1
Ok thanks,I'll try it out later KrOW ..I know what a blob is..my head is kind of loaded at the moment
+ 1
steve Purpose i could explain it but i think thats best for you read docs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
0
@krow do you mean an anchor tag,can you please explain better.
0
steve Purpose I understand 😁
0
KrOW .saw this on the link you sent about creating a blob..
const obj = { hello: "world" };
const blob = new Blob([JSON.stringify(obj, null, 2)], {
type: "application/json",
});
What do the null and 2 mean please?
0
KrOW Well I still can't fathom where 2 and null appeared from.I get all the other processes of JSON.stringify(obj).2 and null are not part of the original object.
0
Optional..Ok I'll take that Tibor