Ant + JSON: How to read JSON from a file, modify it then write it back to the file - json

I need to do this using Ant in an Eclipse project:
Read a JSON file (that is located inside my Eclipse project), parse it, so I can make a change on one of the attributes in there, and finally write the resulting JSON back to the same file.
Is this possible? I have tried using an approach of using with JavaScript, but I can't even reach the file without specifying an absolute path (that I don't want to do, I'd prefer this to be relative to the Ant script.)
Thanks in advance

Related

How to enable "Go to declaration" from typescript to json (i18next)

Im working on a project that uses i18next with react and typescript, where translationkeys are defined in .json files.
One drawback of switching to json for the translation files, is that we can no longer use the intellij idea "Go to declaration" or ctrl + left-click feature, to quickly navigate from a key usage in typescript, to its declaration in the json file.
Is there any way to enable this without requiring all developers to download some third-party intellij plugin?
I've googled for hours for any information about this.
I've made a d.ts file to enable strong typing for where translationkeys are used. What strikes me as odd is that intellij/typescript is able to know when a key doesent exist and warns about it, but at the same time doesent know "where" that key exists whenever i type a correct key.
I also set resolveJsonModule:true in tsconfig, but to my limited understanding it doesent seem relevant.
This is not technically possible because commands like Go To Declaration will look for a declaration in a source code file (think .ts or .js or .d.ts) whereas you want to go ...to its declaration in the json file.
The resolveJsonModule flag won't help you either because as per the docs:
Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. This includes generating a type for the import based on the static JSON shape.
One possible solution is to create a build script to take your .json file and output a .js or .ts file containing the same content, then IDE commands like Go To Declaration will jump to that file.
In summary: you will need some kind of plugin, or a custom build script.
DISCLAIMER: I don't use i18next or react, this answer is based on my understanding of both TypeScript and the JetBrains Rider IDE (which is like IntelliJ).

Extracting attribute values from Vue.js components during build and write to JSON

I want to create a process that extracts attributes from HTML tags in components and stores the values in a JSON file. The idea would be that I'd first add these attributes to my HTML within each Vue component as I write it e.g.
<p custom-attr="value"></p>
Then when I run the npm script to build, it should extract all these attribute values across all files and store within a JSON file local to the project.
I figured I could add a script to package.json scripts which runs when the app builds which is fine. For extracting the attributes, one thought would be to read each Vue file using nodes fs.readFile and then writing in the same way.
Would this be the best option? Is there a better way to do this? Thanks

Read properties file with JSON format

I have a Java code using selenium where I have a properties file which is in JSON format with multiple values and I want to use that file in Jenkins. For that I am using "This project is parameterized" option where I am selecting "File parameters" option.
So My question is How to use the JSON format in Jenkins? Am I doing is correct and what changes we have to make in code for that?
Can anyone help on this?
The "File parameters" is not working in the way you think, it is not like Jenkins will parse file and give you something like key/value map - no.
What is it doing is follwoing , you basically upload file and then how you use it is up to you, so in other words, if that file is for you java code, set the path for that file using the JVM params (e.g. -DpropertiesFilePath = ${abc.xyz}) and then Jenkins will parse the ${abc.xyz} for you and you java code will have proper path to file.
Otherwise, if you want to use the properties inside that JSON file itself for jenkins job configuration needs, then you have to write Jenkins job using either DSL or Jenkinsfile, in which having full access to file you can use for example JsonSlupper and parse Json file and assign properties to stages or whatever you need in Jenkins job walkthrow.

Open local JSON file for examination

I was wondering if it's possible to open a local JSON file so I can just check its structure? Didn't/don't want to upload the file to an online JSON format checker site and was hoping I can just utilize PAW to do that.
Don't seem to be able to do this with a local file, unless I run it through a local server, eg using MAMP, unless I missed something...?
Thanks.
You could copy the content into the txt body then switch to the JSON body this will let you view it in the nice structure, sorry currently no way to directly import a file need to copy past the content.
Take a look at jsonlint npm module. Supports JSON schema validation and pretty printing.

Output reformatted text within a file included in a JSP

I have a few HTML files that I'd like to include via tags in my webapp.
Within some of the files, I have pseudo-dynamic code - specially formatted bits of text that, at runtime, I'd like to be resolved to their respective bits of data in a MySQL table.
For instance, the HTML file might include a line that says:
Welcome, [username].
I want this resolved to (via a logged-in user's data):
Welcome, user#domain.com.
This would be simple to do in a JSP file, but requirements dictate that the files will be created by people who know basic HTML, but not JSP. Simple text-tags like this should be easy enough for me to explain to them, however.
I have the code set up to do resolutions like that for strings, but can anyone think of a way to do it across files? I don't actually need to modify the file on disk - just load the content, modify it, and output it w/in the containing JSP file.
I've been playing around with trying to load the files into strings via the apache readFileToString, but I can't figure out how to load files from a specific folder within the webapp's content directory without hardcoding it in and having to worry about it breaking if I deploy to a different system in the future.
but I can't figure out how to load files from a specific folder within the webapp's content directory without hardcoding it in and having to worry about it breaking if I deploy to a different system in the future.
If those files are located in the webcontent, use ServletContext#getRealPath() to convert a relative web path to an absolute disk file system path. This works if the WAR is exploded in the appserver (most does it by default, only Weblogic doesn't do that by default, but this is configureable IIRC). Inside servlets you can obtain the ServletContext by the inherited getServletContext() method.
String relativeWebappURL = "/html/file.html";
String absoluteFilePath = getServletContext().getRealPath(relativeWebappURL);
File file = new File(absoluteFilePath);
// ...
Alternatively, you can put it in the classpath of the webapplication and make use of ClassLoader#getResource():
String relativeClasspathURL = "/html/file.html";
URL absoluteClasspathURL = Thread.currentThread().getContextClassLoader().getResource(relativeClasspathURL);
File file = new File(absoluteClasspathURL.toURI());
// ...
As to the complete picture, I question if you have ever considered an existing templating framework like Freemarker or Velocity to ease all the job?