I am attempting to arrange variables based on the key of the json nested within another json

It's a bit challenging to condense my goal into one question, but I'll try my best to clarify.

I currently have a JSON array that represents various HTML slider elements categorized within 3 control panes: Basic, Interface, and Advanced. Each pane contains multiple sliders and switches with unique attributes such as "position" and "label".

$json ='[{ "panename": "Basic","pane": "[{\"type\":\"slider\",\"label\":\"Sit Time\",\"id\":\"sitTime\",\"position\":\"0\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Snooze Number\",\"id\":\"snoozeNumber\",\"position\":\"3\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Snooze Duration\",\"id\":\"snoozeDuration\",\"position\":\"4\",\"active\":\"true\"},{\"type\":\"slider\",\"label\":\"Threshold\",\"id\":\"threshold\",\"position\":\"7\",\"active\":\"true\"}]" }, { "panename": "Interface", "pane": ...

My objective is to parse the JSON data efficiently to generate a string of labels sorted by their position values. With a total of 14 sliders/switches, the goal is to arrange them accordingly based on their positions. For instance, sliders 0 and 1 (sitTime, releaseTime) belong to the "Basic" pane while switch 2 (naturalMode) is under the "Advanced" pane. The desired outcome would be something like this:

var labelStr='';
for (x=0;x < number of objects; x++){
    if ('position' == x){
      labelStr= labelStr + ',' +  label of object at position x;
    }
}
//For example:
// if ('position' == 0){
//    labelStr= labelStr +',' + 'sitTime';
//   }

}

In the end, the output string would look like:

labelStr= "sitTime, releaseTime, naturalMode, snoozeNumber, snoozeDuration, volume, lockout ..."  

However, I'm faced with the challenge of parsing the JSON structure effectively to achieve my desired result.

Answer №1

let data ='[{ "panename": "Basic","pane": "[{\\"type\\":\\"slider\\",\\"label\\":\\"Sit Time\\",\\"id\\":\\"sitTime\\",\\"position\\":\\"0\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Snooze Number\\",\\"id\\":\\"snoozeNumber\\",\\"position\\":\\"3\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Snooze Duration\\",\\"id\\":\\"snoozeDuration\\",\\"position\\":\\"4\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"Threshold\\",\\"id\\":\\"threshold\\",\\"position\\":\\"7\\",\\"active\\":\\"true\\"}]"      },      {        "panename": "Interface",        "pane": "[{\\"type\\":\\"slider\\",\\"label\\":\\"Volume\\",\\"position\\":\\"5\\",\\"active\\":\\"true\\"},{\\"type\\":\\"slider\\",\\"label\\":\\"LED\\",\\"id\\":\\"ledBrightness\\",\\"position\\":\\"9\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Notifications\\",\\"id\\":\\"notifications\\",\\"position\\":\\"12\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"Notification Sound\\",\\"id\\":\\"notificationSound\\",\\"position\\":\\"13\\",\\"active\\":\\"true\\"},{\\"type\\":\\"switch\\",\\"label\\":\\"App Alarms\\",\\"id\\":\\"appAlarms\\",\\"position\\":...

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

Assign a class when a path is clicked using Leaflet.js

My goal is to apply a specific class to the clicked polygon by using the following code: function addClass() { function style(feature) { return { className: "active" }; } } function onEachFeature(feature, layer) { ...

Angular Material Popup - Interactive Map from AGM

I am in the process of developing a material dialog to collect user location information using AGM (angular google maps). I have successfully implemented a map on my main page, but when the dialog is opened, it only shows a blank space instead of the map. ...

Is it best to remove trailing/leading whitespace from user input before insertion into the database or during the input process?

There's something I've been pondering that pertains to MVC platforms, but could also be relevant to any web-based platform that deals with user input forms. When is the best time and method to eliminate leading/trailing whitespace from user inpu ...

Preserve content from a hyperlink using JavaScript

How can I store a value to the cache using JavaScript when clicking on an anchor link? The code below sets up the dynamic content: <a align="left" href="projectoverview.html">Test Manager</a> I want to save the text "Test Manager" to the cach ...

Accessing Row Data from a Material Table using a Button Click, Not through Row Selection

My React component features a material table view, shown here: https://i.stack.imgur.com/OUVOD.png Whenever the delete icon is clicked in the table, I want to access the rowdata associated with that particular row. However, all I am able to retrieve is ...

The value of the $scope variable remains constant within the function

I am facing an issue with a scope variable that I passed to a function. Even though the variable is accessible inside the function, its value does not change when I try to modify it. Below is my HTML code: <div class="console_item" ng-class="dropdwns.a ...

What is the best way to retrieve the JSON response in a customized layout?

I have a Module that needs testing, and I am attempting to recreate a json structure like this: $billing_info['billing']->customer_id $billing_info['billing']['source']->exp_year Here is my attempt at recreating it: $ ...

Tips on displaying each element of an array in a unique format within a React component

I am working on creating a component that will display data in boxes. Each set of constant data should be placed within a div for organization. Currently, I have a Box component that is responsible for displaying the data. The Tutorial component receives ...

Is it possible to use the .focus() event on an entire form in JQuery? If so, how

Take a look at this HTML snippet: <form ....> <textarea>....</textarea <input .... /> </form> I'm trying to set up a help section that appears when the user focuses on any form element, and disappears when they lose ...

The React ternary operator within HTML does not display the correct HTML output

I'm currently learning React and facing a challenge with using a ternary operator. My goal is to display a minus sign by default, and then switch it to a plus sign when clicked. I implemented the ternary operator in my JSX and set the initial state of ...

Horizontal scrolling alone is proving to be ineffective

My current HTML code includes a table that is integrated with PHP Laravel. The data is being pulled from Admin.php to populate the table, resulting in it being wider than the screen window. To address this, I added a horizontal scroll bar for navigation pu ...

How can I update an image source using JavaScript in a Django project?

Is it possible to dynamically change the image src using onclick without relying on hard paths or Django template tags? I have concerns that this may not be best practice. How can I implement a method to inject/change the ""{% static 'indv_proj&b ...

Issues with rendering in-line styles in ReactJS after a state update

I'm currently working on implementing a basic state change for a button in React. 'use strict'; class ReactButton extends React.Component { constructor(props) { super(props); this.state = {hovering: false}; } onClick() { ...

Pass on the error to the callback in Node.js

Here is the code snippet in question: User.findById(id, function(err, user) { //blah blah }); The findById method can be found within the User module. Here's a glimpse at its implementation: exports.findById = function(id,callback) { connec ...

Trouble assigning the 'data' attribute to the 'Object' tag using jQuery. [Limited to IE8]

I have encountered a problem when creating an object element dynamically in jQuery to display some content. The code functions perfectly in all browsers except for IE8. Here is the code snippet: j$(document).ready(function(){ j$('.ob ...

What is the most effective method for implementing authorization with Lumen's REST API?

Exploring the creation of a basic REST API with the latest stable version of Lumen for use in mobile applications has me pondering one crucial aspect. When it comes to authorization, what is considered the most effective (and secure) method? Currently, I ...

Error: The JSON input unexpectedly ended, however the PHP file itself is error-free

When trying to display data retrieved using PHP through JSON/Ajax, I encountered an error message: [object Object] | parsererror | SyntaxError: Unexpected end of JSON input The PHP script is functional (I can view the JSON output by directly accessing th ...

What is the best way to choose a random ID from a table within specified time intervals in MySQL?

I need to retrieve a random ID from the table nodes within the last 15 seconds. I attempted the following code, but it produced a lengthy output: const mysql = require('mysql'); const connection = mysql.createConnection({ host ...

Utilize state objects and child components by accessing sub-values within the object

I have a Dropzone component where multiple uploads can happen simultaneously and I want to display the progress of each upload. Within my Dropzone component, there is a state array called uploads: const [uploads, setUploads] = useState([]) Each element i ...

reconfigure keyboard shortcuts in web browser

In the popular web browser Google Chrome, users can quickly jump to the next tab by pressing CTRL + TAB. Is there a way to change or disable this shortcut? The provided code snippet does not seem to work as intended. $(document).keydown(function(e){ ...