0

I have an below json data.I would like to know how to remove empty children in json data ("children":[]) using java script code.Actually I would like to pass this json data in react code for generating dynamic menu.

{
    "items": [
        {
            "Id": 1,
            "name": "Master",
            "url": null,
            "icon": null,
            "Parent": null,
            "children": [
                {
                    "Id": 2,
                    "name": "Single Master",
                    "url": "/base/jumbotrons",
                    "icon": "/theme/colors",
                    "Parent": 1,
                    "children": []
                },
                {
                    "Id": 3,
                    "name": "Double Master",
                    "url": "/base/jumbotrons",
                    "icon": "/theme/colors",
                    "Parent": 1,
                    "children": []
                }
            ]
        }
    ]
}
Peshmerge
  • 1,024
  • 1
  • 14
  • 27
Muhammed Ismail
  • 125
  • 2
  • 12
  • 1
    When generating menu if you get empty array, do not add child tree nodes at all – Justinas Apr 03 '20 at 07:19
  • Does this answer your question? [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Justinas Apr 03 '20 at 07:19
  • Also there is no proper closing brackets. Please fix that as well.. – Maniraj Murugan Apr 03 '20 at 07:21
  • 1) fix your json string because it's invalid now. 2) Convert your json string to an array using `var jsonArray = JSON.parse(JsonString);` 3) Loop through `jsonArray` and filter empty elements! – Peshmerge Apr 03 '20 at 07:32
  • If you are not afraid of regular expressions you can also try this - `myJson.replace(/"[^"]*"\s*:\s*\[\s*\]/,"").replace(/[,\s]+}/,"}")` – IVO GELOV Apr 03 '20 at 07:34

2 Answers2

0

An easy way to is to use regex:

const data = `{
"items": [
    {
        "Id": 1,
        "name": "Master",
        "url": null,
        "icon": null,
        "Parent": null,
        "children": [
            {
                "Id": 2,
                "name": "Single Master",
                "url": "/base/jumbotrons",
                "icon": "/theme/colors",
                "Parent": 1,
                "children": []
            },
            {
                "Id": 3,
                "name": "Double Master",
                "url": "/base/jumbotrons",
                "icon": "/theme/colors",
                "Parent": 1,
                "children": []
            },
]
}
`;

let modified = JSON.stringify(data).replace(/,\\n\s*\\"children\\":\s*\[\]/g,'')

let result = JSON.parse(modified);

console.log(result)
Mechanic
  • 5,015
  • 4
  • 15
  • 38
0

I have tried below code but still its not working.please check my below format and help how to solve this..

const data = {"items":[{"Id":1,"name":"Master","url":null,"icon":null,"Parent":null,"children":[{"Id":2,"name":"Single Master","url":"/base/jumbotrons","icon":"/theme/colors","Parent":1,"children":[]},{"Id":3,"name":"Double Master","url":"/base/jumbotrons","icon":"/theme/colors","Parent":1,"children":[]},{"Id":4,"name":"Vendor Master","url":"/base/jumbotrons","icon":"/theme/colors","Parent":1,"children":[]},{"Id":11,"name":"Colors","url":"/theme/colors","icon":"icon-drop","Parent":1,"children":[]}]},{"Id":5,"name":"Transaction","url":null,"icon":null,"Parent":null,"children":[{"Id":6,"name":"Purchase","url":"/base/dropdowns","icon":"icon-pencil","Parent":5,"children":[]},{"Id":7,"name":"Receipt","url":"/base/dropdowns","icon":"icon-pencil","Parent":5,"children":[]},{"Id":8,"name":"Bills","url":"/base/dropdowns","icon":"icon-pencil","Parent":5,"children":[]}]},{"Id":9,"name":"User Management","url":null,"icon":null,"Parent":null,"children":[{"Id":10,"name":"User List","url":"/base/tables","icon":"icon-puzzle","Parent":9,"children":[]}]}]};

  let modified = JSON.stringify(data).replace(/,\\n\s*\\"children\\":\s*\[\]/g,'')

  let result = JSON.parse(modified);

  alert(result);
Muhammed Ismail
  • 125
  • 2
  • 12