Navigating the JSON Hierarchy: Overcoming uncertainty in the implementation process

I'm currently in the process of developing a website that requires the display of a hierarchy, and I am faced with the restriction of constructing it using JSON.

Before diving into implementation, I find myself torn between two potential approaches for organizing the hierarchy, unsure of which one will offer the most efficient solution for my project.

Project Objective

The goal is to showcase a complete hierarchy on a webpage utilizing HTML5 BoilerPlate, with the hierarchy structure defined within the dico_tree field. This data will need to be parsed using JavaScript to extract all the relevant information it holds.

Key Constraints

  • Items can have either one parent or no parent at all (indicated by null).
  • Items may have multiple children or none at all.
  • The primary focus is to efficiently retrieve each unique item for rapid display on the webpage.

Potential Solutions Considered

Possibility 1

{
    "dico_name" : "Dictionary",
    "version" : "1",
    "dico_tree" : [
        {"ID" : 1,"parent" : null,"children" : [2]},
        {"ID" : 2, "parent" : 1, "children": [3,4]},
        {"ID" : 3, "parent" : 2, "children": null},
        {"ID" : 4, "parent" : 2, "children": null},
        {"ID" : 5,"parent" : null,"children" : [6]},
        {"ID" : 6, "parent" : 5, "children": [7]},
        {"ID" : 7, "parent" : 6, "children": null}],
    "custom_translations_list" : [
        {"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12}
    ] 
}

Possibility 2

{
    "dico_name" : "Dictionary",
    "version" : "1",
    "dico_tree" : [
        {"ID" : 1,"parent" : null,"children" : [
            {
                "ID" : 2, "parent" : 1, "childen": [
                    {
                        "ID" : 3, "parent" : 2, "children": null
                    },
                    {
                        "ID" : 4, "parent" : 2, "children": null
                    }
                ]
            }
        ]},
        {"ID" : 5,"parent" : null,"children" : [
            {
                "ID" : 6, "parent" : 5, "childen": [
                    {
                        "ID" : 7, "parent" : 6, "children": null
                    }
                ]
            }
        ]}
    ],
    "custom_translations_list" : [
        {"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12},
    ]
} 

If further details are required, feel free to ask!

Thank you in advance for your time and assistance :).

Answer №1

Is it necessary for disco_tree to be structured as an array? Consider turning it into an object, which would provide you with O(1) access to any item. Additionally, using an empty array [] instead of null for items without children can help avoid null pointer exceptions.

"dico_tree" : {
       1 : {"ID" : 1, "parent" : null, "children" : [2] },
       2 : {"ID" : 2, "parent" : 1, "children": [3,4] },
       3 : {"ID" : 3, "parent" : 2, "children": [] },
       4 : {"ID" : 4, "parent" : 2, "children": [] },
       5 : {"ID" : 5, "parent" : null, "children" : [6]},
       6 : {"ID" : 6, "parent" : 5, "children": [7] },
       7 : {"ID" : 7, "parent" : 6, "children": [] }
},

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjusting the focus of an element with jQuery based on coordinates and offset values

jQuery.fn.getCoord = function(){ var elem = $(this); var x = elem.offset().left; var y = elem.offset().top; console.log('x: ' + x + ' y: ' + y); ); return { x, y }; }; This custom jQuery funct ...

Encoding child collections within Python using jsonpickle

Is there a way to serialize a python list that contains nested lists? I am using the code below to construct the object for serialization from a gnome keyring. However, I am facing an issue where the jsonpickle encoder does not serialize the child lists. E ...

Having difficulty showing the successful JSON output in either the view or an alert

In my CodeIgniter project, I have three input fields named name, emp_id, and crm_id. I enter the id value and send it to the controller via AJAX and JSON to retrieve all information related to that id. The issue is that while I can see the correct output i ...

Artwork expanding incorrectly on HTML canvas

I'm encountering an issue while attempting to draw on an HTML canvas. I've specified 50 circles and multiple lines within a canvas of size 1000x1000 px, but not all circles are appearing as expected. My assumption is that the elements are being ...

Javascript is responsible for causing a div to become stuck in a loop of alternating between

My current challenge involves a JavaScript function that manipulates boxes by toggling classnames. The strange issue I'm facing is that the correct classes are being set at the correct times, but the div keeps alternating between the original class an ...

Modifying Element Values with JavaScript in Selenium using C#

As a newcomer to automation, I am facing a challenge with automating a web page that has a text field. I initially attempted using driver.FindElement(By.XPath("Xpath of elemnt").SendKeys("Value"); but unfortunately, this method did not work. I then resor ...

Guide on sending an AJAX request for file upload to a Spring MVC controller

Hello, I am looking for assistance with setting up an AJAX call to send a file upload request to the controller in JavaScript. I have a dialog box where I am uploading a file, and I want to trigger the AJAX call upon clicking the upload button. If anyone h ...

The process of running npx create-react-app with a specific name suddenly halts at a particular stage

Throughout my experience, I have never encountered this particular issue with the reliable old create-react-app However, on this occasion, I decided to use npx create-react-app to initiate a new react app. Below is a screenshot depicting the progress o ...

The JavaScript location.reload() function does not seem to be functioning correctly

The issue arises when I use location.reload() in Internet Explorer (IE) and the following error occurs https://i.sstatic.net/SRX3h.png ...

Carousel indicators are not aligned correctly

I have implemented a code snippet to generate a basic carousel slider with 4 items. While navigating to the next slides, everything works smoothly. However, when I try to go back to the first slide using the carousel indicators, the position of the indicat ...

The method continues to receive null values from Ajax despite successfully retrieving the data from Facebook

My current challenge involves creating a login using Facebook. The console indicates that the requested data (email, first_name, etc.) is being retrieved successfully, but for some reason, the AJAX request keeps sending null data to the PHP method. Below ...

Attempting to utilize Vue js to connect to an API

I'm currently facing difficulties while trying to access an API in order to retrieve data. As a novice in the world of vue.js and javascript, I encountered an error message saying Uncaught SyntaxError: Invalid shorthand property initializer. Unfortuna ...

Adjusting the size of Internet Explorer 11 with Java

I am facing difficulty resizing IE11 in kiosk mode. Launching in kiosk mode forces it to go fullscreen, but turning off kiosk mode shows the toolbar & navbar, which I want to prevent a user from editing the URL. Is there a way to achieve this without using ...

Using Three.js to spin a mesh in the direction of a sphere

As a newcomer to Three js, I've been grappling with a challenge lately. My 3D model is currently facing a specific direction, surrounded by a sphere. Before I move the mesh, I want to animate its rotation so that it aligns with the specified sphere. ...

The functionality of the button being disabled on hover and the cursor not responding accurately upon clicking

I need to apply the class name 'btn-common' to all buttons, followed by a common class 'btn' to each button. The width of each button should be set to 100px and the height to 40px, with the border removed and the border radius set to 10 ...

Even though I've specifically instructed it not to, TinyMCE insists on removing any HTML code

I'm encountering difficulties with TinyMCE and custom tags in our custom CMS setup. Here are my current settings: tinyMCE.init({ // General options mode: "textareas", width: "200", theme: "advanced", cleanup_o ...

Update values in a JSON object

Here is a JSON data structure that I am working with: var jsonData = [ { "id": "1", "key1": "Name1", "key2": 2, "key3": 1 }, { "id": "2", "key1": "Name2", "key2": 2, "key3": 1 ...

Transferring PDF files between Vue front-end and Spring Backend via Axios for seamless uploading and downloading

With spring-boot 2.0.4 on the back-end and vue 2.5.16 / axios 0.18.0 on the front-end, my goal is to upload PDF files to the back-end database and retrieve them from the front-end. I initially took inspiration from this example for the spring part: And t ...

Issue encountered when validating password through submission rate limiting

On my login page, I perform validation on the Email and Password entered by the user using submission throttling from the database. However, I encountered an error stating 'undefined index: txtMail' on the validate page. To track the server-side ...

Attempting to increase by one with each visit to the page

I am currently working on a feature that updates a field by incrementing it by +1 every time the page is visited. If the page has never been visited before, I need to add it to the database as a new object in an array. However, my current implementation d ...