How to use complex json api in Datatable

How to use complex json api in Datatable

ayxaayxa Posts: 8Questions: 4Answers: 0

I'm trying to fetch all the information from an API in a Datatable JS. The API Structure is like

{
  "Success": {
    "Meta": {
      "Id": "124578828",
      "Action": "ItemList",
      "Type": "Item"
    },
    "Body": {
      "TotalItems": 18,
      "Item": [
        {
          "Category": "Clothing",
          "ItemId": 12354,
          "Attributes": {
            "name": "Tee 1",
            "brand": "Brand 2",
            "material_type": "Cotton",
            "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
          },
          "Inventory": [
            {
              "Status": "active",
              "stock": 20,
              "Images": [
                "images/tee1_01.jpg",
                "images/tee1_02.jpg",
                "images/tee1_03.jpg",
                "",
                "",
                "",
                ""
              ],
              "Sku": "TEE-001233",
              "Url": "#",
              "price": 20,
              "Available": 12
            }
          ]
        }
      ]
    }
  }
}

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922

    See if this Nested Objects Example helps.

    Kevin

  • ayxaayxa Posts: 8Questions: 4Answers: 0

    Sorry did not helped much :'( :'(

  • kthorngrenkthorngren Posts: 21,170Questions: 26Answers: 4,922
    Answer ✓

    Maybe you can provide more information like what data, from the JSON, do you want to display?

    Here is a example with your JSON:
    http://live.datatables.net/lezocuwe/1/edit

    The Data Manual may help.

    Kevin

  • ayxaayxa Posts: 8Questions: 4Answers: 0

    Thanks !

        $(document).ready(function() {
            $.getJSON("api/ItemList.json", function(data) {
                var table = $('#list').DataTable({
                    data: data.Success.Body.Item,
                    columns: [{
                            data: 'Category'
                        },
                        {
                            data: 'Attributes.name'
                        },
                        {
                            data: 'Inventory.0.Status'
                        }
                    ]
                });
            });
        });
    
This discussion has been closed.