Retrieving data from a child node using Jsonpath

Displayed below is a sample of JSON input data:

{    
 "India":{
      "Attributes":{"Time":"2006-05-04T03:22:11.499Z"},
      "ActiveCity":{
           "profile":"Mumbai"
      },
      "CurrentCities":{
           "Mumbai":{"population":"121212121","Area":8},
           "Delhi":{"population":"121212121","Area":8},
           "Lonng":{"population":"121212121","Area":8}
      },
      "FeatureCities":{
           "NewMumbai":{"population":"121212121","Area":8},
           "NewDelhi":{"population":"121212121","Area":8},
           "NewLonng":{"population":"121212121","Area":8}
      }
 }
}

My goal in TypeScript is to replace the CurrentCities section with only ActiveCity. For example, my desired output should be as follows:

{    
 "India":{
      "Attributes":{"Time":"2006-05-04T03:22:11.499Z"},
      "ActiveCity":{
           "profile":"Mumbai"
      },
      "CurrentCities":{
           "Mumbai":{"population":"121212121","Area":8}
      },
      "FeatureCities":{
           "NewMumbai":{"population":"121212121","Area":8},
           "NewDelhi":{"population":"121212121","Area":8},
           "NewLonng":{"population":"121212121","Area":8}
      }
 }
}

If you have suggestions on how I can achieve this using Jsonpath in TypeScript, please let me know. Thank you!

Answer №1

To retrieve the object stored in currentCities, you can use the current value of:

 obj.India.CurrentCities[obj.India.ActiveCity.profile]

let obj = {  "India": {    "Attributes": {      "Time": "2006-05-04T03:22:11.499Z"    },    "ActiveCity": {      "profile": "Mumbai"    },    "CurrentCities": {      "Mumbai": {        "population": "121212121",        "Area": 8      },      "Delhi": {        "population": "121212121",        "Area": 8      },      "Lonng": {        "population": "121212121",        "Area": 8      }    },    "FeatureCities": {      "NewMumbai": {        "population": "121212121",        "Area": 8      },      "NewDelhi": {        "population": "121212121",        "Area": 8      },      "NewLonng": {        "population": "121212121",        "Area": 8      }    }
}

obj.India.CurrentCities = {[obj.India.ActiveCity.profile]: obj.India.CurrentCities[obj.India.ActiveCity.profile]}

console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Having issues with the functionality of single click in jQuery

I tried to remove the attribute element from a list item, but I am struggling to achieve the desired outcome. When clicking on a list item, it should add a class called "important," otherwise, the class should not exist. Here is my attempt using jQuery: ...

The appropriate method for transferring a prototype to an object

From my perspective, prototypes work like this: let Animal = function() { this.bark = "woof"; } Animal.prototype.barkLoud = function() { return this.bark.toUpperCase(); } let x = new Animal(); x.barkLoud() = "WOOF"; I f ...

Unable to include Electron in a React component for ipcRenderer

I've been attempting to import the ipcRenderer into a react component in order to communicate with the electron side. However, I'm facing issues trying to import electron. Here are some of the attempts I've made: import { ipcRenderer } from ...

Navigating a collection of objects after a button is clicked

My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop: const addBook = (ev) => { ev.preventDefault(); let myLibrary = []; let bookIn ...

Format for storing data using JSON tree structure

Considering how to efficiently store and update data for a tree, let's think about a common scenario like a virtual dom in React. One way to store a tree representing a dom in JSON format could look like this: { nodeId: 'nodeId1', ...n ...

I am looking to pass several parameters through the post method by utilizing ajax

Can someone assist me with this code? I am trying to pass the item ID, stock quantity, and price using AJAX in a POST method. Any help would be greatly appreciated. Thank you in advance! function updateItemDetails(itemId, sellingPrice, stockQty){ v ...

Executing a scroll down action with Selenium in combination with Node.js and the Chai/Mocha testing framework

Browser: Chrome Looking for ways to scroll up or down using Selenium with Node.js (JavaScript). ...

Modify the button's text with jQuery after it has been clicked

I've been struggling to update the text of a button after it's clicked, and I'm encountering an issue where it works in one part of my code but not in another. Could someone please guide me on what might be missing in my code? The first bl ...

The significance of the "? :" operator in JavaScript

These are the lines of code I am currently working with: toggleMore : function( $item, show ) { ( show ) ? $item.find('a.ca-more').show() : $item.find('a.ca-more').hide(); }, Could someone help me understand what this code does? ...

Switch up the key while iterating through a JSON object

Can you modify the key while iterating through objects using an external variable? Picture it like this: var data = [{ "id": 1, "name": "Simon", "age": 13 }, { "id": 2, "name": "Helga", "age": 18 }, { "id": 3, "name": "Tom ...

Storing form data in a database using Codeigniter and AngularJS

Currently utilizing Codeigniter along with AngularJS. Seeking assistance to save $scope.ListTabel into the database. Is it possible for the "FOR" loop to be executed on the server-side in the codeigniter controller rather than in JavaScript? Thank you! $ ...

Insert, delete, and modify rows within the table

I'm struggling with a JavaScript issue and could use some help. How can I add a new row for all columns with the same properties as the old rows, including a "remove" button for the new row? Is there a way to prevent editing cells that contain b ...

What is the best way to refresh the script located within the head tag of an index.html file in an Angular

I've been looking for solutions, but I can't seem to find one. In my index.html file, I've placed some script within the head tag (even above the </body> tag) and included a $(document).ready function. The issue I'm facing is th ...

Unable to retrieve a particular file from S3 while utilizing Strongloop

While I am able to upload, delete, and list folders from an Amazon S3 container using Strongloop, I am facing difficulties retrieving a specific file. This is my code: $scope.getS3Files = function(myfolderName){ //need to fetch all zip files in myfolderA ...

Modifying the value of a variable causes a ripple effect on the value of another variable that had been linked to it

After running the code below, I am receiving values from MongoDB in the 'docs' variable: collection.find({"Stories._id":ObjectID(storyId)}, {"Stories.$":1}, function (e, docs) { var results = docs; results[0].Stories = []; } I ...

Is there a way for me to retrieve the values of <td name="pname"> when the Edit button is

When the button is clicked, I use jQuery to add items in a td that I have created. $("#ddproduct").click(function () { $('#prodcuttable tr:last').after('<tr><td name="pname">' + prodName + '</td> <t ...

Can the presence of required fields be verified using a json scheme?

Currently, I am working on an api endpoint that accepts raw JSON as a parameter. My goal is to validate this JSON using JSON Schema provided by org.everit.json.schema. I specifically aim to enforce a strict structure where only the fields that are explic ...

Is there a method available that functions akin to document.getelementbyid() in this specific scenario?

Currently, I am tackling a project that involves implementing a search function. My initial step is to ensure that all input is converted to lowercase in order to simplify SQL calls. However, I have encountered a challenge that is proving difficult for me ...

What is the best way to ensure elements are rendered in Vue only when they are fully prepared?

Is there a way to delay the rendering of images in my list until all content is ready? I want everything, including text and classes, to finish loading before displaying anything. Even with v-cloak, it's not working! I'm having difficulty with t ...

Sending an HTTP POST request from an Angular 2 client to a Node.js server

I am encountering a peculiar issue with sending POST requests to my Node.js server. Although my GET listeners are functioning perfectly, when attempting to send a simple request from my Angular 2 application (port 4200) to the Node.js server (port 443), I ...