What is the best way to include keys in my JSON data, rather than just values?

I've encountered an issue with the PrimeVue datatable I created, as it is only showing empty rows.

After investigating, I believe the problem lies in my JSON data structure, where the fields do not have keys. What modifications should be made to standardize this JSON so that my data can be properly displayed? Thank you in advance for your assistance.

An example of the current JSON structure:

[
   [
    32,
    "DE BELLOUET Jag",
    "1.3.13.3.",
    "Cid polseruti sau"
   ],
   [
    15,
    "NOURAUD Benjamin",
    "1.3.13.3.",
    "Cid polseruti sau"
   ]
]

An example of the desired JSON structure:

[
   {
    "id":32,
    "fullName":"DE BELLOUET Jag",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   },
   {
    "id":15,
    "fullName":"NOURAUD Benjamin",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   }
]

Main program file: gererPersonnes.vue

... ...

Answer №1

The data you're looking for is not in json format. Here is the information you actually need :

[
   {
    "id":32,
    "fullName":"DE BELLOUET Jag",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   },
   {
    "id":15,
    "fullName":"NOURAUD Benjamin",
    "acs":"1.3.13.3.",
    "nom_service":"Cid polseruti sau"
   }
]

It's important to note the { and } symbols that indicate an Object or a map of properties.

To handle this data effectively, consider creating a POJO (Plain Old Java Object) like this:

public class Agent {
    private Integer id;
    private String fullName;
    private String acs;
    private String nomService;

    // include getters and setters here

}

If you need to return multiple Agents, consider using a List<Agent> in your method. If necessary, explore the Jackson library for serializing a List<Agent> into json format.

Answer №2

To tackle the issue at hand, a new class named AgentServ must be created as the initial step in the solution.

Unfortunately, hibernate presented a hiccup when processing my request, necessitating the use of a list of Object[] to store the results.

Each set of results needed to be carefully cast and incorporated into an agent object before eventually being added to the list.

Finally, the datatable successfully displays the desired outcome.

Here is the finalized code snippet:

public List<AgentServ> listAgentService() {
    String buf = null;
    buf = "SELECT a.id AS id,(btrim(a.nom) || ' ' || btrim(a.prenom)) AS fullName, s.acs AS acs, s.libelle AS nom_service ";
    buf += "FROM Agent a INNER JOIN Service s ON a.service = s ";
    buf += "WHERE ((a.actif = true) OR ((a.actif = false) AND (a.dateSuppression >= CURRENT_DATE)))";

    List<Object[]> query = em.createQuery(buf.toString(), Object[].class).getResultList();
    List<AgentServ> agents = new ArrayList<AgentServ>();
    
    for(Object[] row : query) {
        AgentServ agent = new AgentServ();
        agent.setId((long) row[0]);
        agent.setFullName((String) row[1]);
        agent.setAcs((String) row[2]);
        agent.setNom_service((String) row[3]);
        
        agents.add(agent);
    }
    
    return agents;      
}

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

jquery activating the toggle function to switch between child elements

I'm facing a challenge where I can't use .children() in this scenario, as it won't work since the elements aren't technically children. Here's the snippet of HTML that I'm working with: <p class="l1">A</p> ...

Using AJAX to populate a dropdown menu in a CodeIgniter application

I'm having an issue with using AJAX to populate a dropdown option. Here is the JavaScript code I am using: <script type="text/javascript"> $(document).ready(function(){ $("#sepatu").click(function(e){ e.preventDefault() ...

Explore various date formats using the datepicker functionality

I'm dealing with the code below: <script type="text/javascript" language="javascript" src="~/Scripts/bootstrap-datepicker.min.js"></script> <script type="text/javascript" language="javascript" src="~/Scripts/locales/bootst ...

Ways to split up array objects in an axios GET request

Hello, I recently implemented an AXIOS GET request that returns an array of objects. However, the current example I am using retrieves the entire array at once, and I need to separate the objects so that I can work with them individually. class CryptoAP ...

The attribute 'NameNews' is not recognized in the specified type when running ng build --prod

Definition export interface INewsModule{ IDNews:number; IDCategoery:number; NameNews:string; TopicNews:string; DateNews?:Date; ImageCaption:string; ImageName:string ; } Implementation import { Component, OnInit, Input, I ...

Is Ajax capable of processing and returning JSON data effectively?

I am in the process of making modifications to my codeigniter application. One of the changes I am implementing is allowing admin users to place orders through the admin panel, specifically for received orders via magazines, exhibitions, etc. To achieve ...

Unable to insert rows into an array using sqlite3 in Node.js

I have a snippet of Node.js code that queries a SQLite Database and prints each row individually. The original code is as follows: var sqlite3=require('sqlite3').verbose(); var db=new sqlite3.Database('./database.db',(err)=>{ i ...

How can I create a subdirectory within a parent directory in Azure using Java?

I am developing a Java service that is responsible for creating directories in an Azure storage account. Here is the JSON input: { "accountName" : "azure_account_name", "accountkey" : "azure_account_key", "directoryStructure" : "directory1/d ...

Ways to execute additional grunt tasks from a registered plugin

I am currently in the process of developing a custom Grunt plugin to streamline a frequently used build process. Normally, I find myself copying and pasting my GruntFile across different projects, but I believe creating a plugin would be more efficient. Th ...

Experiencing difficulties establishing a connection between the React client and Node.js server using SocketIO

I am currently getting familiar with socketIO and have successfully set up a basic nodejs server. However, I am facing an issue where my nextJS app is unable to connect to the server as expected. Despite no errors being displayed, the messages that should ...

Using JavaScript to execute a JSON parse function will not work

I am currently working on this code and would appreciate any assistance. I'm trying to retrieve and parse data from my listApp.json file to display a list with one link. As a beginner, I could use some guidance. <script type = "text/javascript"> ...

Reorganizing JSON data with ES6 techniques

I have a scenario where I need to update tire quantities in an array like this: tires: [{ name: "fancyProduct1", quantity: 1 }, { name: "fancyProduct1", quantity: 1 }, { name: "fancyProduct1", quantity: 1 }, { name: "fancyProduct2", quanti ...

Instructions for implementing personalized horizontal and vertical scrolling within Angular 9

I am currently working on an angular application where users can upload files, and I display the contents of the file on the user interface. These files may be quite long, so I would need vertical scrolling to navigate through them easily. Additionally, fo ...

Unlock real-time alerts with the power of JavaScript and PHP!

I am currently working on enhancing my skills in javascript. I have a basic idea of what I want to achieve. My goal is to create an automated javascript function that accesses a php page. This php page will create an array of new notifications for the ja ...

What is the process for moving the final character to the beginning of a string?

Initially, the last letter in the string is being displayed. How can I rearrange it so that the last character appears first in the value? https://i.stack.imgur.com/uGq6H.jpg contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + ( ...

What is the best way to use jQuery to emphasize specific choices within an HTML select element?

Seeking help with jQuery and RegEx in JavaScript for selecting specific options in an HTML select list. var ddl = $($get('<%= someddl.ClientID %>')); Is there a way to utilize the .each() function for this task? For Instance: <select i ...

retrieving information from server via ajax to display on chart

I am currently utilizing the jqPlot JavaScript library for generating graphs and charts in one of my applications, which can be found at . Within my application, there are approximately 5-6 pages where I have integrated this library. However, I would like ...

When sending an AJAX request to a URL, can I verify in the PHP page whether it is a request or if the page has been accessed?

In order to enhance security measures, I need to prevent users from accessing or interacting with the php pages that will be utilized for ajax functionality. Is there a method available to determine if a page has been accessed through an ajax request or b ...

Retrieve the property value from a nested object using a key that contains spaces

Presenting my object: let obj = { innerObj: { "Key with spaces": "Value you seek" } } Upon receiving, I am unaware of the content within obj. I possess a string variable holding the key to access the value. It appears as follows: let ke ...

Troubleshooting date range validation in jQuery

I am facing an issue in my code where I have a set of start and end date fields that need to be validated to ensure the start date comes before the end date. I am currently using the jQuery validation plugin for this purpose. For reference, here is the li ...