I would like to save JSON data to my local pc files.How do I do this with JavaScript,not Node.js? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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

13th Oct 2022, 5:30 AM
steve Purpose
steve  Purpose - avatar
11 ответов
+ 3
You can create a json blob and save it with a simple anchor
13th Oct 2022, 6:06 AM
KrOW
KrOW - avatar
+ 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.
13th Oct 2022, 1:45 PM
Tibor Santa
Tibor Santa - avatar
+ 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
13th Oct 2022, 5:54 AM
Tibor Santa
Tibor Santa - avatar
+ 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
13th Oct 2022, 6:17 AM
KrOW
KrOW - avatar
+ 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
13th Oct 2022, 8:02 AM
steve Purpose
steve  Purpose - avatar
13th Oct 2022, 12:08 PM
KrOW
KrOW - avatar
0
@krow do you mean an anchor tag,can you please explain better.
13th Oct 2022, 6:12 AM
steve Purpose
steve  Purpose - avatar
0
steve Purpose I understand 😁
13th Oct 2022, 8:06 AM
KrOW
KrOW - avatar
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?
13th Oct 2022, 11:35 AM
steve Purpose
steve  Purpose - avatar
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.
13th Oct 2022, 1:35 PM
steve Purpose
steve  Purpose - avatar
0
Optional..Ok I'll take that Tibor
13th Oct 2022, 2:06 PM
steve Purpose
steve  Purpose - avatar