Change JSON File with JavaScript - javascript

Does changing a JSON file with JS acutally affect the JSON file or does it only change the JSON file in temp memory?
Code
user.properties[0].firstName = "Jane";
This is from Replacing a property value in JSON.
Edit
I am not using a server to develop my website, but will be using one when I post it.

That would only affect the json in memory, you would then need to write the changes back to the filesystem for it to update the file contents.

Related

I have a separate .json file, I need to iterate over the file or read through the file and do some stuff. How exactly do I do this in Javascript?

I have a folder with an html, css, json, and js file. The js file are a list of person's and what they do. I need to analyze the json file contents using javacript. I know that in C or C++ you can open the file for reading and analyze it, then close it. So, how do I do this in Javascript? And do I need to add the name of the JSON array in the .json file, or should i leave the contents as it is?
JSON screenshot image
Nothing online have worked thus far. Hopefully Stackoverflow can help.
Let's assume you're using nodeJS, you can import the json file using require .
After you've imported the json file, you can access the object as it would be a JS object.
const jsonFile = require("./pathtofile.json")
for(let obj in jsonFile) {
console.log(obj)
}

Displaying a generated JSON file on a simple website

I have a Java tool, which schedules a task every 24 hours and writes the result in a result.json file. Now I want to display this result.json file on a simple website, but I know that it's not natively possible with JavaScript to access local files. But what other, simple ways exist for this problem? I try to avoid a webservice to keep the scheduling-program and the website on the same server.
Thanks!
You can use the object FileReader for read the file stream from your system and gets the JSON file in a string, later use JSON.parse() to get the JSON in JS object and iterate over it for pretty representing in document DOM, or you can print the string in the HTML without parse to JS object.
Here there are a very completely example of FileReader.

How to store config information for a ASP.NET site not in Web.Config

I need to store a file pairing colours and images for use in my JavaScript. I would have liked to use a simple CSV file and Papa Parse, but PP requires either text or a File object as input, and I can find no way of opening a File object, nor of reading the text from the CSV file. Surely my code should be allowed to read files that reside under the web site, not randomly among the file system?
My alternative is to have the end user, non-technical, edit a JSON file that is parsed by my code.
Am I wrong, or is this the case? Then maybe I should build an editor for the JSON file that simplifies the data editing for the end user.
All the file has to store is colour/image name pairs.
Have you tried using something like this? Are you getting an error? Sorry, I cannot leave comments yet.
StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true);
_testData.WriteLine(TextBox1.Text); // Write the file.
_testData.Flush();
_testData.Close(); // Close the instance of StreamWriter.
_testData.Dispose(); // Dispose from memory.

Need help about accessing a JSON file using JavaScript without the use of any servers

I'm just beginning with JavaScript and I was wondering if I can access a JSON file on the localhost without the use of any servers (like WAMP). I was planning to just read the contents of a JSON file and reflect its contents to an HTML file.
Short answer, yes you can.
Make an XMLHttpRequest against a static file such as: resources/myJSON.json (the extension is merely for organization, you can call the file whatever you want really).
Parse the response as JSON.
Obviously the file must contain a properly formatted JSON object of data, but as long as that's the case, you can load static JSON from a file to "simulate" remote connectivity to a server.
When you're browsing a file as: file:// on your system, it should treat the relative paths correctly.

ExtJS: load JSON from an external file into TreePanel

You can grab data from here. Instead of saving JSON as a separate variable within the script file, is it possible to create a file pointer, and populate a Tree with one config option? Saving JSON in a variable does the job, but it's quite cumbersome!
You can't use files outside of things like AIR it's a security risk and the sandbox won't allow it. You can of course keep the JSON in an external file and request it through HTTP using a TreeLoader with AsyncTreeNode and preloading children OR simply evaluate the AJAX response yourself.

Categories

Resources