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

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

Related

How to convert this large json file into a Python object?

I have this JSON file which contains the data in an interactive map displaying UK food banks run by The Trussell Trust.
food bank data
I am trying to use json.loads() and I keep getting a syntax error. I think Python may be confusing ' with " because every time there's a word like there's it interprets the ``' as the end of a string.
Basically I need to convert the data in that link into any python object.
Edit: fixed the link. Is it possible to get that data and work with it in python or do I need to learn JS?
This should work:
import urllib.request, json
with urllib.request.urlopen("https://www.trusselltrust.org/get-help/find-a-foodbank/foodbank-search/?foodbank_s=all&callback=?") as url:
data = json.loads(url.read().decode()[2:-2])
print(data[0]['foodbank_information']) # example print
The format seems to be JSONP (JSON with padding). json from python does not seem to support this format. If your link changes, especially the callback=? argument, you might want to take a look at this.

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

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.

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.

Pass a java array to javascript

In my app I have an array and I want to pass this array to a javascript script to display an html list.
My app generate this array after reading information from JSON and I need to pass it to javascript. In iOS I used this function: stringByEvaluatingJavaScriptFromString.
How I can do the same in Android?
Use Gson library. It is an amazing JSON parsing library that can parse and create JSON arrays.
https://code.google.com/p/google-gson/
So convert your array to JSON and send it over :)

Export JavaScript Array of Filtered HTML Table data to MS Excel or CSV

I once again need to do something that sounds simple but is infact frustratingly evading me.
On my company's intranet site we have a large table of data that has a javascript filter applied to it so that managers and other interested parties can quickly locate the rows that are relevant to them. The filter I am using can be found at http://tablefilter.free.fr/ .
My issue arises when I need to have a button to export the filtered results to Excel so that the managers can access the data offline. There are many straight forward options for exporting the HTML table to excel but I have been unable to figure out how to get JUST the filtered results to export. (Note: This intranet site will only be accessed via IE)
There is a function as part of the javascript table filter, GetFilteredData(), that will grab the filtered data cells and input these into an array, i think called filteredData[]. This array is formated as such: [rowindex,[value1,value2,value3...]].
So how do I get this array into an Excel or csv file? Again, this will be accessed only by IE so activeX objects and controls are acceptable.
Also I should probably note that I cannot use server-side technologies so please limit your responses to the confines of HTML, javascript and activeX. Thanks!
FYI: DataTables has a nice plugin called TableTools which can export table to csv on client-side. It's achieved using Flash. If you are satisfied with the filter of DataTables, I think this would be a good solution.
http://www.datatables.net/extras/tabletools/
If you are using IE starting at version 8, you can use data: URLs. Just generate the URL and point the borwser there using location.href. The data in CSV can be generated by javascript and base64 encoded.
You might want to consider an approach that relies on string manipulation.
Once you have this array, you can turn it into a JSON string. Try this popular lightweight library (json2.js):
https://github.com/douglascrockford/JSON-js
Example:
text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'
You should have something like
var dataString = '["rowindex",["value1","value2","value3"]]'
At this point you could use some regex replacement on the string to format it to either style.

Categories

Resources