how can i get data from .js file and update this file - javascript

I don't know if this is the best way but I would like to have a .js file with an object that I will update once a day. I would not like to make a database for this because my code already works for the object. Via API I will get the data for the day and I would like to update the .js file. I would like to keep the historic data in this file and use it to feed the website, the API would only be used at the end of the day. It is a website with data from covid-19, I am doing it just for learning, so I am open to new approaches. I try to keep this file in github, but for edit this i need to put my user and pass in code, i dont know how turn around this issue.

Storing JavaScript objects to files is almost always done in JSON format.
Converting an object to a JSON string is done with the JSON.stringify() function.
When you read the JSON string back from the file, you covert it back to a JavaScript object with the JSON.parse() function.

Related

Is it possible to pass a string variable as a file in command line argument?

I'm invoking a command line child process that converts a local file to another file format. It goes like this
>myFileConversion localfile.txt convertedfile.bin
That would convert the localfile.txt to the needed format in a file named convertedfile.bin.
It also has an option to put the contents in stdout.
I'm running this in node js on the server and need the create the localfile.txt on the fly.
The contents of localfile.txt is just a string I dynamically generate. If possible, I would like the pass the string instead of writing the string to a file to be more efficient. How could I do this? Is it possible? Would it be faster than just writing to the local file?
As Chris mentioned in the comments it may be possible to pipe the data but since I only need to save the file once, it's easier to just save the file locally and pass the name of the file.
Please post other possible answers as well!

Best way to convert excel file into a JSON file?

I have data saved in a Microsoft excel file. I need to turn that data into something that a Lambda function can parse.
I think the best way to do this is to convert the excel file into a JSON file (and then my Lambda function can read and parse it).
What's the best way to do this?
To convert the excel data file into a JSON file, I have found some handy online converter tools, like this one. It seems to work.
However, that converter and others add in \r wherever there are line breaks in the data, and \ wherever there are quotes in the data. (the line breaks and especially quotes need to be in the data)
So to properly read the data in the JSON file, I have to then get rid of these changes to the raw data.
Is there another way to do this? Such as a converter that does not change the raw data in this way? Or some method other than a converter?
Once the raw data has been changed (by adding in stuff like \r and \ like I mention above), it becomes cumbersome to remove it. I can do a find/replace to get rid of the changes, but that adds steps that can become costly time wise. And using regex could add performance hits.
**EDIT: Note that I probably need a method that creates an actual document (so a program that produces the data in a client browser would not work). I am looking to create an actual document that my Lambda can then analyze. **
To create a json from excel sheet, I usually prepare the json in excel sheet using excel CONCAT and then copy it. It may not be a perfect way for headers , however it works well for keys and values which generally make up a bigger portion of json.
=CONCAT(CHAR(34),A2,CHAR(34),":",CHAR(34),B2,CHAR(34))
note that CHAR(34) is stands for "
and you can drag it to down from corner for all the rows.

How can i convert CSV file to JSON format in React?

Do i have to make a variable and paste the CSV data and then convert to JSON or please specify some methods.
Thank You!
I'm not familiar enough with react to know if there is a necessity to make it react specific, but if you are open to using a library, this one is superb:
https://github.com/okfn/csv.js/
This library allows you to
Fetch the CSV from a url
Parse that CSV into an array
Serialize it back into CSV format if needed
So for you, I would say, use this library to fetch the data, parse the data, and then just JSON.stringify the array.
If you don't need to do it programmatically, why not just take the CSV, and put array brackets around it, and set your variable as that?
For example, my CSV:
Apple,Banana,Strawberry,
Pickle,Cucumber,Lettuce,
Milke,Bread,Onions,
Then I turn it into an array:
[['Apple','Banana','Strawberry'],
['Pickle','Cucumber','Lettuce'],
['Milk','Bread','Onions']]
You can just use built in Excel functions like concatenate to turn it into this format if your CSV file is really big and this is just a one-off exercise

JSON file parsing issue due to javascript variables

I've been provided with a json file to show results in a tableview.
But when I tried json file validation using online json file editor then I am getting "Build tree failed" error.
I posted this message to my client about wrong json file and then he has posted some strange description which I am totally unaware.
Yes the json is not a pure json. It has javascript variables which have json variable. For now u can use a webview to evaluate the javascript variables and use the json value. For ex : dp_f : corresponds to list of departure train is a javascript array variable. U may have to eval the variable and store each array element is a new json with key as dp_f and value of array of these values
Please tell me how can I use this json file which is not pure. What is the way to parse such files ?
The only real way to evaluate JavaScript (which is really what you have, not JSON) is to evaluate JavaScript.
You will have to use some form of eval(). I recommend trying a JavaScript sandbox, if available to you. Of course the best option would be to get the data fixed.

How to do Javascript access a local database in txt format

I am newbie working on a 100% js prototype. It consist of 3 docs: an html page full of xml tags, a small dictionary in a text file format, and a js file with jquery.
The js needs to parse the xml tags (no problem here) and look into the mini-dictionary list for available translations.
Which is the best way to implement the mini-dictionary list. (No more than 50.000 records). Is there a way to load the list into a memory database and access it from js? Which is the usual path to take in this case? What is the simplest and machine-independent way to do this?
Any directions as to where should I research are greatly appreciated.
I would suggest encoding mini-dictionary with JSON data format, and then using AJAX to get that file and parse it. But then you are risking someone will just copy whole dictionary and steal your work.
That is, if you are not using server side language, like PHP. If you are using it, then just store everything into database and request just specific words with AJAX.

Categories

Resources