Apache NiFi: read json file in ECMAScript - javascript

I am new to NiFi. My question is how can we read json file in ECMAScript while using excute script process. I want to load json file with mapping json for schema mapping.

I have an post on my blog about how to read in a flow file containing JSON using ExecuteScript with ECMAScript:
http://funnifi.blogspot.com/2016/03/executescript-json-to-json-revisited.html
There is also another StackOverflow question similar to your use case of reading in a mapping file and applying it to incoming data (using Groovy):
Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping file
Not sure if this helps too but I have another post on validating JSON with ExecuteScript (but Groovy not ECMAScript) using JSONSchema: http://funnifi.blogspot.com/2016/05/validating-json-in-nifi-with.html

Related

Search through files in path using only javascript

I'm coding a webpage that needs to read some data from different csv on a path depending on the country of the user.
the path is something like this:
./csv/m2-2022-10-25_13_45_55_es.csv
m2-2022-10-25_13_45_56_fr.csv
m2-2022-10-25_13_46_04_it.csv
etc
And those files will be replaced regularly, the only that we'll always have is the country code (es, fr, it, etc).
So, what I need is to list all the files on the path to an array, and loop through the array to find if the last characters of the filename are $countryCode + ".csv", and there run some code.
But I can't find how, all the solutions I find are using Node.js, but are there a solution using only Javascript (or jQuery)?
Regards!
You cannot use pure Javascript to do that, because if you wanted to search files in your computer only using javascript, it would be a huge security breach.
You must use node.js to open files but you can make an API to your nodejs file from your javascript and you can send as a response the content of your file.
Here some links that might help you :
FS : https://nodejs.org/api/fs.html
NodeJS api : https://medium.com/swlh/how-to-create-a-simple-restful-api-in-node-js-ae4bfddea158
You can check a similar question here:
Get list of filenames in folder with Javascript
You can't access to filesystem from the frontend, this it would be a huge security breach, because anyone could access to your filesystem tree.
You have to do a function in backend to build the array you want and send it to frontend.
If you create a function in backend file that returns the array of files in the folder, you can call it from the frontend via XMLHttpRequest or Fetch to get the array in frontend and be able to use in your js file.

Displaying a generated JSON file on a simple website

I have a Java tool, which schedules a task every 24 hours and writes the result in a result.json file. Now I want to display this result.json file on a simple website, but I know that it's not natively possible with JavaScript to access local files. But what other, simple ways exist for this problem? I try to avoid a webservice to keep the scheduling-program and the website on the same server.
Thanks!
You can use the object FileReader for read the file stream from your system and gets the JSON file in a string, later use JSON.parse() to get the JSON in JS object and iterate over it for pretty representing in document DOM, or you can print the string in the HTML without parse to JS object.
Here there are a very completely example of FileReader.

How to read & write in a properties file with javascript

I'm working in a JEE web project and i have a problem, i would like to read and write into a properties file which is located into WebContent/WEB-INF/classes folder and i need to do this with javascript, do you have an idea how to do that ?
Assuming you mean JavaScript on the client: Create an endpoint that the JavaScript code can post to via ajax, and have servlet code handle the post by updating the properties file.

How to access a local JSON file with Javascript?

I have a Python program that generates an html page for reporting results. The html page is saved in an output directory on disk alongside a javascript file that helps with dynamic table handling. I also save a JSON file to this output directory that I would like to read in with my javascript file. This JSON file has data from the Python run (saved dictionary) that I would like to be able to access. So in an output directory on disk I have:
C:/somedirectory/output/report.html
C:/somedirectory/output/tables.js
C:/somedirectory/output/data.json
All files have been created from my program.
My html page has a table with checkboxes and if those checkboxes are selected I would like to update a second table based on data saved in the JSON file. Thus I would like to open my html report in any browser and read in the JSON file as a javascript object.
I have been trying to use ajax and .getJSON but am getting the
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
I have searched and seen many similar problems but have not come across anything that quite fits what I need. Thoughts and a work around would be greatly appreciated. Thanks.
Update
Since everything is run locally on the client side I have decided to embed the JSON data (python dictionary) and javascript code directly into the html report output. This way the data is internally accessible and the html file can be passed around without dependency issues. The user with the answer I selected below has a link that eludes to this solution.
JavaScript runs on the client machine, hence it can only access files on the client machine using a special setup.
If you want it to read JSON on your server, you should use the path:
http://example.com/output/data.json
Better way would be to read/write JSON file from Python and then send the table data to JavaScript as in this answer: Send data from Python to Javascript (JSON)

Need help about accessing a JSON file using JavaScript without the use of any servers

I'm just beginning with JavaScript and I was wondering if I can access a JSON file on the localhost without the use of any servers (like WAMP). I was planning to just read the contents of a JSON file and reflect its contents to an HTML file.
Short answer, yes you can.
Make an XMLHttpRequest against a static file such as: resources/myJSON.json (the extension is merely for organization, you can call the file whatever you want really).
Parse the response as JSON.
Obviously the file must contain a properly formatted JSON object of data, but as long as that's the case, you can load static JSON from a file to "simulate" remote connectivity to a server.
When you're browsing a file as: file:// on your system, it should treat the relative paths correctly.

Categories

Resources