Expanding JSON response with additional properties through the utilization of the jQuery File-Upload plugin within the Struts 2 framework

I am currently utilizing the jQuery File-Upload plugin in conjunction with Struts 2.

Within my action, I am populating the JSON object "results". This is the only information I wish for my action to return.

However, the plugin is also including the file object which results in an incomplete JSON response and leads to issues in my callbacks. (Please be aware that if I do not populate my result object, it will return a valid JSON "file" object.

Is there a way to prevent the "file" JSON response from being returned? My intention is for the action to solely return the"results"

{
    "results": [
        {
            "ExcelPath": "/usr/test/test.xlsx",
            "ExcelName": "test.xlsx",
            "TestExcelStatus": "success"
        }
    ] 
}
{
    "file": {
        "absolute": true,
        "absoluteFile": null,
        "absolutePath": "\/usr\/local\/apache-tomcat-7.0.39\/temp"
    },
    "path": "\/usr\/local\/apache-tomcat-7.0.39\/temp\/up

The code snippet from my Action displays as follows:

org.json.JSONObject resp = new JSONObject();            
JSONArray resultsArray = new JSONArray();
resp.put("results",resultsArray);
JSONObject result = new JSONObject();           
result.put("TestExcelStatus", "success");
result.put("ExcelName", this.fileFileName);
result.put("ExcelPath", fileToCreate.getPath());
resultsArray.put(result);
    

servletResponse.reset();
servletResponse.setHeader("Content-Type","application/json; charset=UTF-8");
servletResponse.setHeader("CacheControl","no-cache");
servletResponse.setHeader("Pragma","no-cache");
servletResponse.setHeader("Expires","-1");
//resp.writeJSONString(out);
resp.write(servletResponse.getWriter());

Despite anticipating that the line below would clear the existing JSON content and only showcase my "results", it continues to display everything.

servletResponse.reset();

The desired outcome for the JSON response should resemble the following, excluding the "file".

{
    "results": [
        {
            "ExcelPath": "/usr/test/test.xlsx",
            "ExcelName": "test.xlsx",
            "TestExcelStatus": "success"
        }
    ] 
}

Answer №1

When utilizing Struts, the action will consistently provide a result code that is utilized by the action invocation to trigger a configured result. It appears that this particular result may be in JSON format.

If the action returns a JSON result, the default setting for the "root" property will be populated with data from the action instance or a model if a model-driven action is being used.

This implies that all public properties of the "root" object will be serialized into JSON. The serialization process can be controlled through annotations on the properties or by configuring the result parameters to include/exclude specific properties from the final result.

In reality, all you need to do is configure the "root" parameter in the result to return an object that can be serialized into JSON format. However, if you are directly writing to the response, you have the flexibility to change the result code returned by the action to "NONE," which signifies that no specific result is being returned.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring techniques to iterate through this specific JSON data within an Angular frontend

I am looking for assistance in looping the data below into a 'Select dropdown' using Angular 10. The data consists of all the States in India along with their districts. I have retrieved this information from the internet, but I am unsure how to ...

Virustotal API - Retrieve complete Subdomain Inventory

I am searching for a way to retrieve the complete list of subdomains for a given URL by utilizing the Virustotal API When I tested Google.com on their platform, Virustotal reported a total of 3.2k subdomains for Google.com. Visit this link for more detail ...

Discovering password values within JSON objects through the use of regular expressions

In my possession is a substantial JSON object that houses numerous JSON entities, many of which adhere to the following structure (key: sometext-v1.password, value: password for example: "apps":[ "settings": [ ...

Using Twitter Bootstrap Modal for Rails form submissions with AJAX/JSON

I'm encountering a challenge with integrating a Rails form_for form into a Bootstrap modal and updating a table using AJAX. I've attempted to follow the guidance provided at , but without success. I have also explored the suggestions in the resou ...

Reformatting the JSON Data in Python Post-Retrieval

I've been facing a challenge with this issue for quite some time now and it has negatively impacted my productivity. Currently, I am utilizing python to initiate a session with an api and retrieve its data. The URL follows the format http://api.kiva ...

Python: Transforming Json Timestamp to Datetime

Is there a way to convert the datetime string '2021-11-21T18:57:18.000+01:00' from JSON to a datetime object in Python? timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ") When I try this code snippet, it throws ...

Retrieving specific values from a JArray containing Lists of JInts

Currently, I am honing my skills in Scala and Akka Streams while working with the Stripe payment API. My focus is on extracting the transaction date labeled created from the Json response. Here's an example of the response (displaying 2 transactions) ...

What is the best way to select a specific value from JSON (Webhook) Data?

I am looking for a way to extract and store a specific value from a JSON data into a variable. Specifically, I want to save the value of Name (John) in a variable like this: var name = "". I attempted using var name = data.Name but it is not wor ...

Combining two sets of data in MongoDB

Looking for a way to merge two collections into one while ensuring that duplicates aren't included in the final result. Any suggestions on how to achieve this? I've heard about aggregation and map reduce methods, but I'm not sure which one i ...

Ways to insert user data into a hidden input field

I am facing an issue with the input field on my website. Users can enter their desired input, and this data is copied into a hidden input field. However, the problem arises when new data replaces the old data. This is where I copy all the data: $('# ...

Using Json.Net: Learning to Exclude Null Elements When Deserializing Array JSON Data

My JSON data looks like this: { "Variable1": "1", "Variable2": "50000", "ArrayObject": [null] } In my code, I have the following stubs: public class Class1 { public string Variable1 { get; se ...

Operations such as OR and AND can be used within a JSONB array to filter

Imagine having a table named temp (jsondata jsonb) In Postgres, there is a method to query a jsonb array object for containment check using SELECT jsondata FROM temp WHERE (jsondata->'properties'->'home') ? 'football&apo ...

Using jQuery to Populate a Table with JSON Data

Despite my extensive search and analysis of similar questions on this forum, I have reached a roadblock. My script is failing to load data from a Json file into the table I am trying to create, even after closely following the jquery API guidelines. Any as ...

Locate and retrieve all the records that contain an asterisk symbol in MongoDB

Could someone provide assistance with finding documents in MongoDB that contain the '*' character using regex? For example, the following regex (db.collection.find{value: new Regex('*')}) should retrieve all documents with '*&apos ...

Utilizing JSON parse/stringify for data manipulation

I'm seeking advice on manipulating JSON data using JavaScript, particularly with the stringify/parse methods. In this scenario, I start by creating a JSON string and then use parse to convert it into an object. However, my goal is to efficiently delet ...

"Trouble arises when attempting to include tables in the as_json

These are the models I am working with. class User < ActiveRecord::Base has_many :kriya_sessions end class KriyaSession < ActiveRecord::Base belongs_to :user end In my users controller, I am attempting to do the following: def user_sessions ...

Using Golang to extract data from JSON objects with unspecified keys

I am encountering an issue while attempting to unmarshal json data with dynamic field keys into a specific struct. The json data is retrieved from the storcli utility for linux. One part of the code functions correctly, however, when the json data contai ...

Transfer information to firebase using a Java class and fetch a single attribute in a different activity

Firebase Database LinkI've been encountering an issue while trying to store data in Firebase using a particular code snippet. Previously, I was able to save data by creating new children under users>uid without any problems. However, now that I am att ...

Sending a JavaScript function as part of an ajax response

I'm currently working on a system where the user can submit a form through AJAX and receive a callback in response. The process involves the server processing the form data and returning both an error message and a JavaScript function that can perform ...

The JSON data did not fully transfer into my AngularJS application

I wrote the code to display country details, but I am only getting partial information from the JSON. I am only able to fetch the country name and population in the output. Could there be an issue with the syntax? <!DOCTYPE HTML> <html ng-app= ...