JSON file parsing issue due to javascript variables - javascript

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.

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.

Assign json stored in a file to a js variable

I am working on a project and I was not able to figure out how to do this. I have a json file and need to assign it to a js variable to use the protovis visualization tools.
I tried searching on google but could not find any. Please let me know if someone knew how to do this. Thanks!
Use jQuery's parseJSON().
Description: Takes a well-formed JSON string and returns the resulting
JavaScript object. version added: 1.4.1jQuery.parseJSON( json )
jsonThe JSON string to parse. Passing in a malformed JSON string may
result in an exception being thrown.
If you're getting the file from your server via the XMLHttpRequest object, you can parse the json using the JSON.parse function:
var myJson = JSON.parse(response);
For browsers that don't support the JSON object you can get a library for it here: http://www.json.org/js.html

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.

reading a json object in jsp

i have a JSON object passed to the jsp page. it is passed as a string. now i have to parse this string and retrieve the values that are passed through the JSON object. so that i can print the values in the same jsp.
There's lots of resources, including libraries & plugins for various technologies/frameworks on json.org.
With tons of JSON parsers, it comes down to how you want to deal with data in JSON. My personal favorite from the lot is Jackson, but many others work well for simple cases too, including the "reference implementation" (aka JSON.org parser).
(I assume you want a Java parser, given reference to jsp)
My preferred solution to this problem involves using a JSON parser that provides an output that implements the java.util.Map and java.util.List interface. This allows for simple parsing of the JSON structure in the JSP Expression language.
Here is an example using JSON4J provided with Apache Wink. The sample imports JSON data from a URL, parses it in a java scriptlet and browses the resulting structure.
<c:import var="dataJson" url="http://localhost/request.json"/>
<%
String json = (String)pageContext.getAttribute("dataJson");
pageContext.setAttribute("parsedJSON", org.apache.commons.json.JSON.parse(json));
%>
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}
To make this clean, it would be preferable to create a JSTL tag to do the parsing and avoid java scriplet.
<c:import var="dataJson" url="http://localhost/request.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}

Categories

Resources