Iterating through nested json

Iterating through nested json

BatkoBatko Posts: 2Questions: 1Answers: 0

Hi there,

I was wondering if someone could help me with this.

I am trying to utilise values within the "Client" aspect of the Json below.

I can access this using {"data": "Client[0].Name"}, however there could be multiple items within this list which I want to unpack into the cell. So ideally I'd have each name separated by a comma. As the amount of items is variable, I can't use "Client[0].Name", "Client[1].Name" etc.

I have tried Client.Name and Client..Name but neither of these seem to work.

Any ideas?

Thanks in advance for any help anyone can offer!

{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"Company_Name": "Example",
"Website": "http://www.example.com",
"Client": [
{
"id": 1,
"Name": "Client",
"Account": 2
},

        ],
        "tags": {}
    },

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954
    Answer ✓

    Use columns.render to loop through the array of client names to build the comma separated list of names. Use a standard Javascript loop to iterate the list of objects. Something like this:

    {
      data: 'Client',
      render: function ( data, type, row ) {
        var names = [];
        data.forEach(function( clientData ){
          names.push( clientData.Name );
        });
        return names.join( ',' );
      }
    },
    

    I didn't test this so it might need adjustments to work.

    Kevin

  • BatkoBatko Posts: 2Questions: 1Answers: 0

    Worked straight away, thank you Kevin, I really appreciate it. Have a great day!

Sign In or Register to comment.