Can you provide guidance on extracting both the key and value from a nested JSON object?

Here is a JSON object example:

JSONObject childDepenObj = new JSONObject();

In this object, keys and values are added as follows:

childDepenObj.put("COL_CHLD_NULFY", gridObjectArray[86]);
childDepenObj.put("COL_CHLD_MAN", gridObjectArray[87]);

Now, I have another JSON Object:

JSONObject gridObject = new JSONObject();

I am using the first JSON object as a value in the above object:

gridObject.put("hiddenObj", childDepenObj);

My question is how to access the key and values of the first JSON object from the frontend,

When trying to access it like this: response.hiddenobj;

The response is coming under hiddenObj. If I want to directly access the key and value like: response.COL_CHLD_NULFY; It shows as undefined. Can anyone help me with this?

Answer №1

If I am interpreting your request correctly, you can retrieve the desired value by accessing

response.hiddenObj.COL_CHLD_NULFY;

However, if you prefer to access it like this

response.COL_CHLD_NULFY;

then simply return the childDepenObj as the response.


This is how the current JSON response appears:

{
  "hiddenObj": {
    "COL_CHLD_NULFY": <<gridObjectArray[86] value>>,
    "COL_CHLD_MAN": <<gridObjectArray[87] value>>,
  }
}

This is the desired format:

{
  "COL_CHLD_NULFY": <<gridObjectArray[86] value>>,
  "COL_CHLD_MAN": <<gridObjectArray[87] value>>,
}

Answer №2

    JSONObject newChildObj = new JSONObject();
    newChildObj.put("CHLD_ITEM", gridObjectArray[86]); 
    newChildObj.put("CHLD_SIZE", gridObjectArray[87]);

    JSONObject mainObject = new JSONObject();
    mainObject.put("hiddenData", newChildObj);

    result = response.hiddenobj.CHLD_ITEM

Give this a shot...

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

Tips for avoiding swiper from interacting with the content upon reaching the final swiper slide button

Can anyone help me with a problem I'm experiencing? When navigating using the next or previous buttons, I always come across one button that is grayed out, indicating that I cannot move forward (for the next button) or backward (for the prev button) a ...

Retrieving JSON Array Information Using PHP Objects

stdClass Object ( [id] => 8586332 [email] => <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f373a3333301f32263a323e3633713c3032">[email protected]</a> [optInType] => Unknown [emailType] => Html [ ...

What is the process for loading an HTML form into a specific target using Angular?

Is there a way to load an HTML form into a target in Angular without the use of jQuery? I have a working script, but I am currently stuck on: <html> <head> <script src="components/angular/angular.js"></script> <script&g ...

Record changes in another sheet when Google Spreadsheet is edited

Can anyone provide assistance with a problem in Google Spreadsheet? I am looking to log the name and timestamp when a value in a specific column in the "Venues" sheet is changed. However, I'm having trouble determining if I'm working with the co ...

Stacking Android view layout components on top of each other

I encountered an issue with the layout of my Android Activity. Initially, I had a navigation drawer and two Activities working within a frame layout which was functioning perfectly. However, due to problems with the navigation drawer, I had to move the fra ...

What is the process for adding texture to a model that has been imported using OBJLoader?

I've been immersed in my little project using three.js, but I'm facing a challenge with mapping textures on objects loaded by THREE.OBJLoader. Interestingly, there are no issues with three.js built-in geometry. I find myself quite perplexed... / ...

Expand and enhance your content with the Vue Sidebar Menu plugin

Recently, I integrated a side-bar-menu utilizing . My goal is to have a sidebar menu that pushes its content when it expands. Any suggestions on which props or styles I should incorporate to achieve this effect? Below is my Vue code: <template> ...

Displaying components according to ternary conditional operator

I'm currently working on a piece of code that I only want to render when the correct or incorrect answer is given. Initially, I want to display only the question and the possible answers. In React, when the answer is "false", the message "you are wron ...

I am on a mission to locate the exact attribute of the object within an array

I am struggling to locate the instance of Motorola in the array 'clientes' that is within a switch statement inside a for loop. The exercise requires finding the name and identification of the first customer who buys a Motorola cellphone. I belie ...

Submitting data twice through AJAX POST requests

Using a PHP file called via AJAX to insert data into MySQL. Prior to the insert statement, the PHP code runs a select query to check for duplicate records and then proceeds with the insert statement. Problem: When calling the PHP file from AJAX, it gets ...

DataFrame conversion from JSON file resulted in a ValueError: Encountered an unexpected character while decoding an array value (2)

I am dealing with a large JSON file that follows this structure: [{"faceId": "2cb5a26a-1acc-4eb2-8c39-8e05e604f057", "faceRectangle": {"top": 54, "left": 125, "width": 78, "height": 78}, "faceAttributes": {"smile": 0.584, "headPose": {"pitch": 0.0, "rol ...

In JavaScript, would you like to loop through a nested object and assign an empty value of "" to each key, and then generate an array of these modified objects?

Basic Element : elem = { "site": "{{basic_siteId}}", "elementDetails": [ { "subElementInfo": "{{basic_elementInfo}}", "category": "other", "picture": "abc.jpg", "attributes": { ...

Obtaining a record in SugarCRM using a criteria other than its ID

Although the question may not be clear, my issue is actually quite simple. According to the SugarCRM documentation, in order to retrieve a record, I need to send a GET request to /<module>/:recordId. My goal is to fetch a record based on its email ...

Filter array to only include the most recent items with unique names (javascript)

I'm trying to retrieve the most recent result for each unique name using javascript. Is there a straightforward way to accomplish this in javascript? This question was inspired by a similar SQL post found here: Get Latest Rates For Each Distinct Rate ...

Include the URL as a parameter in the query when utilizing Tesseract OCR

I have successfully implemented the tesseract ocr, but I am wondering if it is possible to run tesseract with a URL as a parameter. I want to achieve the following: localhost/test.html/?othersite.com/image/image2.jpg Here are some image URLs for demonst ...

I am looking to send the JSON data to the backend in Grails by utilizing square brackets instead of curly brackets

this is the back-end section in Grails used to fetch data def addMessageA() { JSONObject requestJson= request.JSON; Message message = new Message(); message.message = requestJson.message; message.category = Category.get(requestJson.cId); ...

Performing an Angular JS $http post request with URL parameters included in the GET request

I am running into an issue when trying to post data with GET parameters to a URL using Angular's $http method. Here is the URL I am trying to post to: http://localhost/api/?r=page/product However, my attempts at posting data to this URL using AJAX h ...

Splunk bubble diagram - a unique way to visualize data

I'm attempting to generate a bubble chart in Splunk using the code below: <dashboard> <label>Bubble Chart</label> <row> <panel> <chart> <searchString>index = _inter ...

Creating an Icosahedron with realistic behavior

I am currently working on creating a d20, which is a dice with 20 sides or an icosahedron. Here is how I have been approaching it: const IcosahedronDice = (props) => { const [ref] = useBox(() => ({ mass: 1, position: [0, 10, 0] })); return ( ...

Executing a .jar file specified in the pom.xml file from a cloned project within a Dockerfile

In my Dockerfile, I have the following: FROM maven:3.3-jdk-8-alpine RUN mkdir -p /usr/src/app WORKDIR /usr/src/app RUN apk add --no-cache curl tar bash wget git RUN git clone https://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-c ...