So I'm trying to program something using Node.js, I've got a file that is called 'profile.json', that is an object. When something happens, I need to update a value of 'name' to a new name. So I do
'profile.name = name2;'
but after I restart my program everything comes back and I have to change it again. So my problem is how would I save the json after updating it?
It is not saving because you are reading the file and updating it in the application. However, you are not changing anything in the file. Once you read the file and parse the JSON, no link exists to the original file. The JSON exists only in memory. You will want to use the NodeJS File System class to write the file. https://nodejs.org/api/fs.html First, check if the file exists, if it does delete it (or move/rename). Second, save the file using the fs.writeFile method.
Related
I wrote an app that fills a database from a local JSON file, and the app doesn't use this file after that.
Is there a way to remove it by code (by deleting it for example)?
Thanks.
I am trying to do some optimization in ADF. Setup is a third-party tool copies one JSON file per object to a BLOB storage container. These feed to a Mapping Data Flow. The individual files written by the third party tool work great. If I copy these files to a different BLOB folder using an Azure Copy Data activity, the MDF can no longer parse the files and gives an error: "JSON parsing error, unsupported encoding or multiline." I started this with a Merge Files, but outcome is same regardless of copy behavior I choose.
2ND EDIT: After another day's work, I have found that the Copy Activity Merge File from JSON to JSON definitely adds an EOL character to each single JSON object as it gets imported to the Merge file. I have also found that the MDF fails definitely with those EOL characters in the Merge file. If I remove all EOL characters from the Merge file, the same MDF will work. For me, this is a bug. The copy activity is adding a character that breaks the MDF. There seems to be a second issue in some of my data that doesn't fail as an individual file but does when concatenated that breaks the MDF when I try to pull all the files together, but I have tested the basic behavior on 1-5000 files and been able to repeat the fail/success tests.
I took the original file, and the copied file, ran them through all of sorts of test, what I eventually found when I dump into Notepad++:
Copied file:
{"CustomerMasterData":{"Customer":[{"ID":"123456","name":"Customer Name",}]}}\r\n
Original file:
{"CustomerMasterData":{"Customer":[{"ID":"123456","name":"Customer Name",}]}}\n
If I change the copied file from ending with \r\n to \n, the MDF can read the file again. What is going on here? And how do I change the file write behavior or the MDF settings so that I can concatenate or copy files without the CRLF?
EDIT: NEW INFORMATION -- It seems on further review like maybe the minification/whitespace removal is the culprit. If I download the file created by the ADF copy and format it using a JSON formatter, it works. Maybe the CRLF -> LF masked something else. I'm not sure what to do at this point, but its super frustrating.
Other possibly relevant information:
Both the source and sink JSON datasets are set to use UTF-8 (not default(UTF-8), although I tried that). Would a different encoding fix this?
I have tried remapping schemas, creating new data sets, creating new Mapping Data Flows, still get the same error.
EDITED for clarity based on comments:
In the case of a single JSON element in a file, I can get this to work -- data preview returns same success or failure as pipeline when run
In the case of multiple documents merged by ADF I get the below instead. It seems on further review like maybe the minification/whitespace removal is the culprit. If I download the file created by the ADF copy and format it using a JSON formatter, it works. Maybe the CRLF -> LF masked something else. I'm not sure what to do at this point, but its super frustrating.
Repro: Create any valid JSON as a single file, put it in blob storage, use it as a source in a mapping data flow, to do any sink operation. Create a second file with same schema, get them both to run in same flow using wildcard paths. Use a Copy Activity with Merge Files as the Sink Copy Activity and Array of Objects as the File pattern. Try to make your MDF use this new file. If it fails, download the file created by ADF, run it through a formatter (I have used both VS Code -> "Format Document" from standard VS Code JSON extension, and VS 2019 "Unminify" command) and reupload... It should work now.
don't know if you already solved the problem: I came across the exact same problem 3 days ago and after several tries I found a solution:
in the copy data activity under sink settings, use "set of objects" (instead of "array of objects") under File Pattern, so that the merged big JSON has the value of the original small JSON files written per line
in the MDF after setting up the wildcard paths with the *.json pattern, under JSON Settings select: Document per line as the Document form.
After that you should be good to go, as least it solved my problem. The automatic written CRLF in "array of objects" setting in the copy data activity should be a default setting and MSFT should provide the option to omit it in the settings in the future.
According to my test:
1.copy data activity can't change unix(LF) to windows(CRLF).
2.MDF can also parse unix(LF) file and windows(CRLF) file.
Maybe there is something else wrong.
By the way,I see there is a comma after "name":"Customer Name" in your Original file,I delete it before my test.
I write this code and it append my json file with all data again.
After this content of my file become twice.
QFile file("C:/Users/Vizexperts/Documents/QT projects/Json_File_Read_Write/new 1.json");
file.open(QIODevice::ReadWrite|QIODevice::Text);
QString dataString=file.readAll();
QJsonDocument doc= QJsonDocument::fromJson(dataString.toUtf8());
QJsonObject dataobject =doc.object();
qDebug()<<(dataobject)["D"].toString();
QJsonValue valueA=dataobject.value(QString("A"));
QJsonObject valueAobject=valueA.toObject();
QJsonValue valueA1=valueAobject["A1"];
qDebug()<<valueA1.toString();
ui->label->setText(valueA1.toString());
dataobject["D"]="a";
doc.setObject(dataobject);
file.write(doc.toJson());
file.close();
There is no easy way to edit a single value in a JSON file and then writing it back.
The easiest (in terms of prone to errors) way is to
Open file
Load text into JSON object
Edit data
Write JSON object back to file
When you open a file for writing and want to overwrite the existing data in it, you need to also use the QIODevice::Truncate flag. This truncates the text inside your JSON file and whatever you write to that file erases the previous content.
If you want, you can open the file twice (and close it accordingly) - once when you read from it (so no writing access to it) and once when you write to it. This way you can handle the given operation's specific errors that may occur. If error handling is not something you want to bother yourself with, just add the extra flag and you are good to go.
It appears you are rewriting the JSONdoc to file.
file.write(doc.toJson());
As far as I know there is no way to just update the file with the change you did in the code. The easy way is to clear the file and rewrite the whole object again.
A bigger challenge would be to parse the file again, compare the JSON objects, find the cursor position where you need to write the changes and update the file. Depending on the size of the JSON object, this seems over-engineered.
I have CSV DataSet Config where I have location of my .csv file
Tried both by writing the absolute path and putting my file in the same directory. Below is the screenshot
CSV file gets stored but the next line says it must exist
Below is mine HTTP Request
HTTP REquest
By running this in command Line nothing is getting stored in the my output file
I have tried that too.
Check following things:
Name of the file is correct. In case of Unix file names are case sensitive so check the name of the file and name you have mentioned in CSV data set configuration.
Keep the CSV file in the same folder where your JMX resides, and just mention file name in CSV dataset config. This avoids confusion in future even if you move the script to any other box.
Also make sure in the file there is no new line character at the end of the file. That will result in error.
Try a fully qualified path. ~/... was not working for me either - /temp/file.csv did the trick
I created a new Azure WebJobs project which is a console app. I placed a settings.json file in the root and I'm trying to access it using the following code but I keep getting an error that says it cannot locate the file. I think it's looking for it under Debug folder but I don't want to move the file there. How do I reference that file?
var config = new Configuration();
config.AddJsonFile("settings.json");
I tried "~/settings.json" but that didn't work either.
You need to identify if it's a deployment or runtime issue, per this article.
Make sure that your file is in fact getting deployed:
In VS, check that it has Copy to Output directory set to Copy if Newer
Use Kudu Console to look at the relevant WebJob folder under D:\home\site\wwwroot\App_Data\jobs\... and make sure that the json file made it to there next to the exe.
You can try to add your json file into your WebJob project's Resources as shown:
Remember to set the file type as Text and encoding to UTF-8.
In your code, you can easily access your json file as string as below:
// The Resources property depends on your actual file name being referenced
var settingsJson = Resources.settings;
Hope this helps!