Problem with Json value

Problem with Json value

silenssilens Posts: 101Questions: 40Answers: 0

I have a json like this, the problem is that when I put in column the field proy_sim.name or prsp_def.name I get an error. The other fields work well.

This is my Json

"prsp_sol": [
        {
            "proy_sim.name": "Vehículos",
            "prsp_def.name": "TRACTOR",
            "prsp_def": 2613,
            "his_sim": 72
        }
    ]

the colum:

"columns": [
            {
                {"data": "proy_sim.name"}, 
                {"data": "prsp_def.name"}, 
                {"data": "prsp_def"}, 
                {"data": "his_dim"}, 
            }
                    
        ]

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @silens ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    "proy_sim.name" indicates to Datatables that this is a nested object as shown in this example:
    https://datatables.net/examples/ajax/deep.html

    Datatables will expect the data structure to look more like this:

    "prsp_sol": [{
            "proy_sim": {
                "name": "Vehículos"
            },
            "prsp_def": {
                "name": "TRACTOR"
            },
            "prsp_def": 2613,  << duplicate key
            "his_sim": 72
      }]
    

    Although that would result in a duplicate key as noted.

    Instead of periods can you use another character like underscore, for example: "proy_sim_name": "Vehículos",?

    Also note that you have an extra set of {} in your columns.data:

    "columns": [
                { // invalid
                    {"data": "proy_sim.name"},
                    {"data": "prsp_def.name"},
                    {"data": "prsp_def"},
                    {"data": "his_dim"},
                } // invalid
                         
            ]
    

    Kevin

  • silenssilens Posts: 101Questions: 40Answers: 0

    Thank you very much for your response, I can not change the character, I am desperate already with this problem, would there be a way to fix it without changing the character?

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    edited May 2019 Answer ✓

    Just looked at the columns.data docs and it states this:

    . - Dotted Javascript notation. Just as you use a . in Javascript to read from nested objects, so to can the options specified in data. For example: browser.version or browser.name. If your object parameter name contains a period, use \\ to escape it - i.e. first\\.name.

    Sounds like your change should look like this:

    "columns": [
                    {"data": "proy\\_sim.name"},
                    {"data": "prsp\\_def.name"},
                    {"data": "prsp_def"},
                    {"data": "his_dim"},
                          
            ]
    

    Kevin

This discussion has been closed.