Increasing the depth of JSON objects through multiple levels

Looking to expand upon an existing empty JSON object.

After a couple of basic extensions, I end up with {size: 10, from: 0}.

However, when attempting to use the $.extend function with multi-level JSON

"query" : 
  { "query_string" : 
    { "fields" : ["name", "description"], 
      "query" : "d.search.value", 
      "tie_breaker" : 0 
    }
  }

I am facing difficulty achieving success.

            data : function ( d ) {
                var query = '{}';

                sQuery = $.extend(query,{size: d.length});
                sQuery = $.extend(sQuery,{from: d.start});
                
                if ( d.search.value ) {     
                
                    sQuery = $.extend(sQuery,                   
                        "query" : { "query_string" : { "fields" : ["name", "description"], "query" : "d.search.value", "tie_breaker" : 0 } }
                    );
                    console.log(sQuery);
                };
                    
            return JSON.stringify(sQuery);
            
        },                      

But I'm encountering a "

Uncaught SyntaxError: missing ) after argument list
" error.

I think I have overlooked something simple but cannot seem to pinpoint it.

Answer №1

Shoutout to @Barmar for providing the ultimate solution:

$('#integrations').DataTable( {
    var query = '{}';

    "bProcessing": true,
    "bServerSide": true,
    "ajax": {
        "url": "http://localhost:9200/integrations-prod/_search",

        data : function ( d ) {
            console.log( d.length );
            sQuery = $.extend(query,{size: d.length});
            sQuery = $.extend(sQuery,{from: d.start});
            
            if ( d.search.value ) {     
                sQuery = $.extend(sQuery,                   
                {"query" : { "query_string" : { "fields" : ["name", "description"], "query" : d.search.value, "tie_breaker" : 0 } }}
                );
            };
            return JSON.stringify(sQuery);
        },
        "contentType": "application/json",
        "type": "POST",
        "dataType": "json",
        "dataSrc": "hits.hits",
        dataFilter: function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.hits.total.value;
            json.recordsFiltered = json.hits.total.value;
            return JSON.stringify( json ); // return JSON string
        },
        "columnDefs": [ { "defaultContent": "-", "targets": "_all" } ],
    },
    "columns": [
        { "data": "_source.name" },
        { "data": "_source.description" },
        { "data": "_source.valid" },
        { "data": "_source.status_id" },
        { "data": "_source.region_id" },
        { "data": "_source.validated_time" }
    ]
} );

This incidentally also resolves scenarios like Elasticsearch Datatables, Datatables pagination with Elasticsearch, dataTable custom loading contents, ...

Huge shoutout to @Barmar

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

The function you are trying to call is not callable. The type 'Promise<void>' does not have any call signatures. This issue is related to Mongodb and Nodejs

Currently, I am attempting to establish a connection between MongoDB and Node (ts). However, during the connection process, I encountered an error stating: "This expression is not callable. Type 'Promise<void>' has no call signatures" da ...

Troubleshooting error: Unable to access property 'post' in Angular Service when using $http

As I delve into learning Angular, my approach involves shifting all the business logic to services. However, I encountered an error while attempting a post request within a service: Cannot read property 'post' of undefined Provided below is a ...

Tips for utilizing jQuery to check for a value within an input field?

Hey there! I'm currently teaching myself jQuery in order to achieve a specific effect. I'll do my best to explain my issue, but please bear with me as English is not my first language. My current goal is to create an input field that will disapp ...

Instructions on how to save HTML "innerHTML" along with attributes to a text document

I'm currently developing an HTML export feature from a DIV tag that includes various elements and attributes. HTML: <div id="master"><span class="classname">content goes here</span></div> <span class="download" onclick="ca ...

issue with implementing the chart.js npm package

Just recently, I added chart.js to my project using npm. My goal is to utilize the package for creating graphs. npm install chart.js --save After the installation, I attempted to import the module with: import chart from 'Chartjs'; However, t ...

Unveil Secret Divs with a Click

I am in search of a way to display a hidden div when I click on a specific div, similar to the expanding images feature in Google's image search results. I have made progress with my limited knowledge of javascript, as shown in this CodePen: http://co ...

Is there a way to easily uncheck all MaterialUI Toggle components with the click of a button or triggering an outside

If you have a set of <Toggle /> components in an app to filter clothes by size, and you want to reset all the filters with a single click on a button rather than unchecking each toggle individually. Is it possible to achieve this using materials-ui ...

Lookup all users in the database using search criteria in MongoDB

Currently, I am attempting to search for a user based on their name field using insomnia for testing purposes. In my schema for users, it looks like this: const userSchema = mongoose.Schema({ id: { type: String }, name: { type: String, require ...

a guide to transforming data into a string with json using angular

I am struggling to figure out how to bind my form data into a JSON string. My situation involves using a string field in the database and saving checkbox values in a single database column using JSON. I have created an HTML form, but I am unsure of how to ...

When I click the button, the loading.gif fails to appear

I'm having trouble getting my loading screen to display when my button is clicked. The Ajax function works fine, but the loading screen is not showing up. CSS <style> #display_loading{ position: absolute; top: 50%; left: 50%; transform ...

What's the reason behind JavaScript's Every method not functioning properly?

I'm struggling to understand why the array method 'every' is not functioning properly in my project (working on a roguelike dungeon crawler game). Here's an example of the array of objects I am working with: { x: newrm.x, ...

Extract all nested array object values in JavaScript without including a specific key value

I am looking for a way to remove a specific key value pair within a nested array object in JavaScript. The goal is to get all the objects in the array after the removal. Can someone help me with this? In the following object, I want to remove the mon key ...

Clear Vuex state upon page refresh

In my mutation, I am updating the state as follows: try { const response = await axios.put('http://localhost:3000/api/mobile/v3/expense/vouchers/form_refresh', sendForm, { headers: { Accept: 'application/json', 'C ...

Texture Elusion not being rendered

I'm experiencing difficulties with rendering items in my Forge mods. The error message I am encountering is as follows: [16:14:07] [Client thread/ERROR] [FML]: An exception occurred while loading the model for variant "skycastle:itemskygem#inventor ...

Developing a Simple Analytics Dashboard

As I plan to develop a fundamental Analytics page to delve deeper into Javascript, AJAX and alternative data storage methods like redis, I find myself pondering the optimal way to deliver user data. Should it be dynamically calculated at real-time for gr ...

Accessing data in JSON format from a URL

I'm working on a website that analyzes data from the game Overwatch. There's this link () that, when visited, displays text in JSON format. Is there a way to use JavaScript to read this data and display it nicely within a <p> tag on my si ...

Troubleshooting Issue with Public File Serving in ExpressJS Deployment (NodeJS/Express)

The directory structure of my project is organized as follows: my_project server.js app.js | | - public | ----------| images | javascripts | stylesheets | -------------- imgs ---scripts.js ---styles.css | | - routes | | - views | ...

Guide on utilizing personalized fonts with Handlebars and Puppeteer

I have a Handlebar template that I am converting to PDF using Puppeteer. My question is how can I incorporate custom fonts? Currently, I have a static folder declared in my app.js file like this: app.use(express.static(path.join(__dirname, 'assets&ap ...

Eliminating redundant values from a JSON object that is nested within another

Currently, I am working on rendering a list of Labels from a local JSON file. However, I am facing the issue of duplicates and I want each label to appear only once. I attempted to use Array.filter() and other methods to achieve this line: "Categories": ob ...

Validation of multiple textboxes can be achieved by utilizing either JavaScript or Angular 1

How can I implement a validation in Angular 1 or JavaScript that will enable the submit button only when there is text in any of the 3 textboxes? If no text is present, the button should be disabled. Below is the code snippet: First name: <br> < ...