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

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.

Related

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.

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

Flash Twitter API with JSON

I have read a lot about parsing JSON with Actionscript. Originally it was said to use this library. http://code.google.com/p/as3corelib/ but it seems Flash Player 11 has native support for it now.
My problem is that I cannot find examples or help that takes you from beginning to end of the process. Everything I have read seems to start in the middle. I have no real experience with JSON so this is a problem. I don't even know how to point ActionScript to the JSON file it needs to read.
I have a project with a tight deadline that requires me to read twitter through JSON. I need to get the three most recent tweets, along with the user who posted it, their twitter name and the time those tweets were posted.
The back end to this is already set up I believe by the development team here, therefor my JSON files or XML just needs to be pointed to and then I need to display the values in the interface text boxes I have already designed and created.
Any help will be greatly appreciated...I do know that there are a lot of threads on here I just do not understand them as they all have some understanding of it to begin with.
You need to:
Load the data, whatever it is.
Parse the data from a particular format.
For this you would normally:
Use URLLoader class to load any data. (Just go to the language reference and look into example of how to use this class).
Use whatever parser to parse the particular format that you need. http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/JSON.html this is the reference to JSON API, it also shows usage examples. I'm not aware of these API being in production version of the player, still there might be quite a bit of FP 10.X players out there, so I'd have a fallback JSON parser, but I would recommend using this library: http://www.blooddy.by/en/crypto/ over as3corelib because it is faster. The built-in API are no different from those you would find in browser, so if you look up JSON JavaScript entries, the use should be in general similar to Flash.
After you parse JSON format, you will end up with a number of objects of the following types: Object, Array, Boolean, Number, String. It has also literals to mean null and undefined. Basically, you will be working with native to Flash data structures, you only should take extra care because they will be dynamically constructed, meaning you may not make assumption about existence of parts of the data - you must always check the availability.
wvxvw's answer is good, but I think skips over a to be desired explanation of what JSON itself is. JSON is plain text, javascript object notation, when you read the text on screen it looks something like this
http://www.json.org/example.html
you can see a side by side JSON and XML (both plain text formats) essentially JSON is a bunch of name value pairs.
When you use JSON.parse("your JSON string goes here") it will do the conversions to AS3 "dynamic objects" which are just plain objects (whose properties can be assigned without previously being defined, hence dynamic). But to make a long story short, take the example you see in the link above, copy and paste the JSON as a string variable in AS3, use
var str:String = '{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}';
var test:Object = JSON.parse(str);
method on the string, store it in a variable and use the debugger to see what the resulting object is. As far as I know there's really nothing else to JSON it's simply this format for storing data (you can't use E4X on it since it's not XML based and because of that it's slightly more concise than XML, no closing tags, but in my opionion slightly less readable... but is valid javascript). For a nice break-down of the performance gains/losses between AMF, JSON and XML check out this page: http://www.jamesward.com/census2/ Though many times you don't have a choice with regard to the delivery message format or protocol being used if you're not building the service, it's good to understand what the performance costs of them are.

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