How to parse this nested json

How to parse this nested json

maikimaiki Posts: 3Questions: 1Answers: 0

Hi,
I am working on displaying json data in DataTable.
My json looks like this.

    "Results": [
        {
            "DocId": "4842-1398-5949",
            "EnvId": ":Q21:k:p:o:7:~190718162425318.nev|1",
            "DocNum": 1,
            "CustomAttributes": [
                {
                    "Id": 1001,
                    "Value": [
                        "OGC - Admin"
                    ]
                },
                {
                    "Id": 1005,
                    "Value": [
                        "Watch List Project Tracking"
                    ]
                },
                {
                    "Id": 1003,
                    "Value": [
                        "AVR"
                    ]
                },
                {
                    "Id": 1004,
                    "Value": [
                        "Watch List FY20"
                    ]
                },
                {
                    "Id": 6,
                    "Value": [
                        "My Name 1"
                    ]
                },
                {
                    "Id": 5,
                    "Value": [
                        "2019-07-18 20:24:25Z"
                    ]
                },
                {
                    "Id": 8,
                    "Value": [
                        "My Name 1"
                    ]
                },
                {
                    "Id": 7,
                    "Value": [
                        "2020-02-13 20:43:57Z"
                    ]
                },
                {
                    "Id": 11,
                    "Value": [
                        "xlsx"
                    ]
                }
            ]
        },
        {
            "DocId": "4819-9709-4576",
            "EnvId": ":Q19:c:1:j:d:~200109120634702.nev|1",
            "DocNum": 1,
            "CustomAttributes": [
                {
                    "Id": 1001,
                    "Value": [
                        "Research Office"
                    ]
                },
                {
                    "Id": 1005,
                    "Value": [
                        "NIH Inquiry - Sotomayor"
                    ]
                },
                {
                    "Id": 1003,
                    "Value": [
                        "CJO"
                    ]
                },
                {
                    "Id": 1002,
                    "Value": [
                        "Chart/Spreadsheet"
                    ]
                },
                {
                    "Id": 6,
                    "Value": [
                        "My Name 2"
                    ]
                },
                {
                    "Id": 5,
                    "Value": [
                        "2020-01-09 17:06:34Z"
                    ]
                },
                {
                    "Id": 8,
                    "Value": [
                        "My Name 2"
                    ]
                },
                {
                    "Id": 7,
                    "Value": [
                        "2020-02-13 20:28:40Z"
                    ]
                },
                {
                    "Id": 11,
                    "Value": [
                        "docx"
                    ]
                }
            ]
        }
]

I have to display data in CustomAttributes.
Each ID value correspond to a title (which will be table column names) and value will be column value.
I have a list of 11 ids and the result json will have data for any of those 11 ids.
Any pointer on how to go about setting up the table will be really helpful.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 8,082Questions: 25Answers: 1,816

    Not sure I totally understand what you are after but hopefully this example using columns.render will help:
    http://live.datatables.net/noqitewe/1/edit

    Kevin

  • maikimaiki Posts: 3Questions: 1Answers: 0

    Kevin,
    Thanks for your quick response.
    I am having trouble figuring out how to get the data when we have variable properties.
    Not all the records will have same number of properties.
    For example, One row will have this property, the next row may not have this.

                    {
                        "Id": 1002,
                        "Value": [
                            "Chart/Spreadsheet"
                        ]
                    }
    

    But I know there are 11 possible properties. One row might have 8 out of 11 properties. Another row might have only 5 out of 11.
    Hope this is clear enough to understand.

    Thanks,
    Maiki

  • kthorngrenkthorngren Posts: 8,082Questions: 25Answers: 1,816
    Answer ✓

    You can use a for loop to iterate the array to find the matching Id for each column. Like this example where I changed one of the Id: 1005 to Id: 0:
    http://live.datatables.net/noqitewe/2/edit

    A better option is to return CustomAttributes as an object instead of an array then you wouldn't need to loop through it.

    Kevin

  • maikimaiki Posts: 3Questions: 1Answers: 0

    Kevin,
    Thanks for your help. I am able to get the data by looping through the array.
    Its a third party rest api returning json. I don't have option to modify it.

    Thanks,
    -Maiki

Sign In or Register to comment.