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

Navigating JSON with Java/Jackson

I am currently working on a Java application to parse JSON data from the Reddit API. The sample JSON data I am trying to parse is structured like this: [ { "kind": "Listing", "data": { "modhash": "1jq62oyvwe15aaba7eb18b0b4363b567a007507663 ...

What is the best way to include numerous optional parameters within a single route in Express?

Been a huge fan of Stackoverflow and finally decided to ask my first question here! I've been working on a JavaScript Express project, trying to figure out if it's possible to achieve the desired functionality under a single GET request. Struggli ...

Utilize Selenium Webdriver to automate clicking all the way to the end of a carousel

Is there a way to continuously click on the next arrow of a carousel until reaching the end of the loop using Selenium WebDriver? I tried using a for-loop for the click event, but it's not working. Below is the basic code snippet for locating the elem ...

Using an array of JSON objects to set up a Backbone.js bootstrap-initialized application

Trying to bootstrap a backbone collection by using an array of JSON objects has led to some unexpected errors. When attempting to call reset on the collection object, an error from Backbone is thrown - Uncaught TypeError: undefined is not a function. Inte ...

What is the best way to connect a date and time value with the flip-countdown feature in Vue.js within

I am endeavoring to connect datetime with the flip-countdown component so that it can recognize the date and time for commencing the countdown. The issue I'm facing is related to the inability of the component to properly interpret the time, indicati ...

Retrieve the thousand separator for numbers using Angular in various languages

When using the English locale, numbers appear as follows: 111,111,222.00, with a comma as the thousand separator and a point as the decimal separator. In languages like German, the same number would be represented as 111.111.222,00, reversing the positions ...

Is Jquery Steps causing interference with the datepicker functionality?

I am currently using the jquery steps plugin for creating a wizard on my website. However, I am experiencing some issues with the functionality of the datepicker and qtip inside the steps. Even after switching the .js references, the problem still persists ...

How to position an absolute element beneath a fixed element

My website is experiencing a problem where the fixed header is overlapping an absolute paragraph on this page. Does anyone know how to resolve this issue? ...

What is the best way to utilize JSON data in React components?

I am looking to showcase a collection of icons similar to what I saw on this particular website. Within my JSON data, I have stored both the family and name values for these icons. What I aim to do is map this JSON data in order to pass the family and nam ...

Choosing an embedded iframe using selenium in javascript with node-js

When working with the selenium webdriver module in node-js, I encountered an issue trying to select a nested iframe within another iframe. Here's an example scenario: <iframe id="firstframe"> <div id="firstdiv"></div> <ifr ...

Using jQuery to filter or duplicate options in a select box - a step-by-step guide!

I am struggling with filtering options in two select boxes. One contains projects and the other contains people: <select id="project"> <option data-person_ids="[75,76,77]">None</option> <option data-person_ids="[77]">Project A& ...

Having trouble getting images to display in Vue markdown?

I am currently utilizing the vue-markdown package for rendering markdown content. The package works fine except for images, which do not display as expected. Interestingly, when using an img element with the correct file path, the image shows up without an ...

Getting up and running with the NICE DCV SDK: A beginner's guide

Exploring the NICE DCV SDK provided by AWS has been my latest project. You can find the documentation here. However, I've hit a roadblock while trying to run the example code mentioned in the docs. My attempt to execute it on a node server resulted in ...

Display array elements in a PDF document using pdfmake

Upon reaching the final page of my Angular project, I have an array filled with data retrieved from a database. How can I utilize pdfmake to import this data into a PDF file? My goal is to display a table where the first column shows interv.code and the ...

Is there a way to conceal the contents of a page until all the images have finished loading?

I'm currently working on improving the performance of a website that is loading very slowly. I have already reorganized, compressed and minified the JavaScript and CSS files, but the main issue seems to be with the images. The site contains large imag ...

Is it possible to assign a value to the "input" that is different from the Vue variable?

I am looking to dynamically change the display of an input based on the value retrieved from a database for the variable: paragraph.RCBulletinNumber I have experimented with using the computed method, however, it seems that this approach may not be suitab ...

The request.files property in express-fileupload is consistently coming back as undefined

I am trying to achieve the task of uploading a file from my browser and sending it via POST to an Express.js application, which will then download the file using express-fileupload. Here is the client-side JavaScript code I have written so far: // Triggere ...

Having trouble getting videos to load on VueJS?

I've been grappling with loading a video from a local folder in my Vue.js 2 and Laravel project, but haven't had any luck so far. The Vue-loader plugin is properly installed and added in my webpack.config.js as per the documentation: I first at ...

Unable to render pages with ng-view in Angular.js

I am facing an issue with my Angular.js application where the pages are not loading when using ng-view. When I type the URL http://localhost:8888/dashboard, the pages should be displayed. Here is an explanation of my code: view/dashboard.html: <!DO ...

How can one decrease the size of a React project?

After running npx create-react-app my-app, the download was quick but then it took over an hour for the installation process to complete. Upon checking the size of the newly installed project, I found that the node_modules folder alone was a whopping 4.24 ...