A guide on extracting certain fields and values from a JSON structure

I received a JSON output from the response body that contains various details about a contract.

My goal is to extract only two specific pieces of information:

  1. "contractId"
  2. "contractStatus"

To achieve this, I'm utilizing JavaScript to parse and retrieve the data accurately.

{
  "value" : [ {
    "title" : "Test Ariba IT contract",
    "description" : "",
    "commodities" : [ {
      "uniqueName" : "3.99",
      "domain" : "custom",
      "name" : "OTHER ENGINEERING SERVICES"
    } ],
    "regions" : [ {
      "uniqueName" : "700",
      "name" : "US Procurement"
    } ],
    "departments" : [ ],
    "owner" : {
      "uniqueName" : "Gautam.KumarBans",
      "passwordAdapter" : "PasswordAdapter1",
      "name" : "Gautam Bansal",
      "emailAddress" : "Gautam.KumarBansa",
      "organization" : "NaTEST",
      "orgANId" : "AN01002048300-T",
      "orgName" : "NatioST",
      "timeZoneID" : "America/New_York",
      "localeID" : "[ariba.basic.core.LocaleID [BaseId 1497 AAAAAB7XZ 15l.3f]]"
    },
    "contractId" : "CW1948133",
    "contractStatus" : "Draft",
    "supplier" : {
      "name" : "[CM] KIER GROUP",
      "systemID" : "50066293",
      "smVendorID" : null,
      "organizationIDs" : [ {
        "domain" : "buyersystemid",
        "value" : "50066293"
      } ],
      "address" : {
        "name" : "50066293",
        "uniqueName" : "50066293",
        "phone" : "",
        "fax" : "",
        "city" : "",
        "state" : "",
        "postalCode" : ""
      }
    },
    "effectiveDate" : "2020-12-21T00:00:00.000+0000",
    ...
    "maximumNumberOfRenewals" : 0,
    "autoRenewalInterval" : 0,
    "isTestProject" : true,
    ...
}

Please note that the provided JSON object includes a lot of additional details beyond just the desired "contractId" and "contractStatus".

Answer №1

const data = {
  "value": [{
    "title": "Test Ariba IT contract",
    "description": "",
    "commodities": [{
      "uniqueName": "3.99",
      "domain": "custom",
      "name": "OTHER ENGINEERING SERVICES"
    }],
    "regions": [{
      "uniqueName": "700",
      "name": "US Procurement"
    }],
    "departments": [],
    "owner": {
      "uniqueName": "Gautam.KumarBans",
      "passwordAdapter": "PasswordAdapter1",
      "name": "Gautam Bansal",
      "emailAddress": "Gautam.KumarBansa",
      "organization": "NaTEST",
      "orgANId": "AN01002048300-T",
      "orgName": "NatioST",
      "timeZoneID": "America/New_York",
      "localeID": "[ariba.basic.core.LocaleID [BaseId 1497 AAAAAB7XZ 15l.3f]]"
    },
    "contractId": "CW1948133",
    "contractStatus": "Draft",
    "supplier": {
      "name": "[CM] KIER GROUP",
      "systemID": "50066293",
      "smVendorID": null,
      "organizationIDs": [{
        "domain": "buyersystemid",
        "value": "50066293"
      }],
      "address": {
        "name": "50066293",
        "uniqueName": "50066293",
        "phone": "",
        "fax": "",
        "city": "",
        "state": "",
        "postalCode": ""
      }
    },
    "effectiveDate": "2020-12-21T00:00:00.000+0000",
    "agreementDate": "2020-12-21T00:00:00.000+0000",
    "expirationDate": "2021-12-21T00:00:00.000+0000",
    "creationDate": "2020-12-21T21:11:10.486+0000",
    "contractAmount": {
      "amount": 1000.0000000000,
      "currency": "USD"
    },
    "version": 1,
    "templateId": "CW1945605",
    "amendmentType": "",
    ...
    }],
  "count": null,
  "start": null,
  "warnings": null
}

const new_data = data.value.map(({ contractId, contractStatus }) => {
  return { contractId, contractStatus };
});

console.log(new_data);

For more information on JSON, visit this link: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON

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

The application is experiencing extended loading times during production

After deploying my Angular 2 app on Heroku, I've noticed that it's taking a long time to load. Is there a way to bundle everything up instead of having all the scripts and stylesheets scattered across my index.html file? <html> <head> ...

What is the best method to send the HTML button ID to a Python file through Django?

I have the following HTML code snippet that generates dynamic buttons: {% for i in loop_times %} <button type="submit" class="'btn btn-lg" name="{{ i }}" id="{{ i }}" onclick="alert(this.id)" formmethod="post"><a href="{% url 'button ...

Image not showing up when using drawImage() from canvas rendering context 2D

Need help with drawImage() method in JavaScript <head> </head> <body> <script type = "text/javascript"> var body, canvas, img, cxt; body = document.getElementsByTagName("body" ...

Styling a dynamically-injected div using an AJAX request (xhrGet)

Hello everyone, currently I am using the code below to fetch updated content after receiving a "push" event from a server. The new content is then used to replace the existing div/content. However, I'm facing an issue where none of my styles from the ...

Extract particular "set" from JSON retrieval

For instance, here is a sample output, but the JSON response will be much longer. I am only interested in filtering out NFTs with a specific contract address (collection) from this response. Currently utilizing axios and typescript in React. { "own ...

Loading an ASP.net page on the client side

Can someone tell me the event for pageload on clientside? function onPageLoad() { setSelectedIndexTo(1); } I attempted to do this, however no changes occur. Appreciate any help. Thanks! ...

Is there a way to activate the window load event within this Jest test scenario?

I'm in the process of setting up my first Jest test for DOM manipulation and jQuery in my Rails project. Right now, I'm facing a basic challenge of ensuring that imported JavaScript functions are functioning as expected. In order to tackle this ...

Prevent popup content from refreshing: Tips for creating a dynamic user experience

Currently, I am working on a website that is similar to Facebook. One of the features I am implementing is the ability for users to like posts or photos and see who has liked them. However, I am facing an issue with the popup that shows the list of people ...

Creating a communication bridge between a Chrome extension and an Angular application on a webpage

There's a chrome extension I've been working on that alters the Dom of webpages. However, I'm dealing with a page built in Angular, which means I need to adjust the scope of the element. How would I go about doing this? ...

Looking to grab a single value from a JSON file and utilize it in component code within Angular 2 (8.2.8)?

My JSON file contains information about various pages: { "pagesList": [ { "pageUrl": "index", "imgUrl": "homepage", "imgNumber": 17 }, { "pageUrl": "second", "imgUrl": "secondimage", ...

How to achieve a seamless iframe effect using CSS

With the introduction of new HTML5 iframe attributes, there are now more options available. One such attribute is the seamless attribute, which has the ability to: Create an iframe that appears to be seamlessly integrated into the containing document. ...

How do I specify a unique directory for pages in Next.js that is not within the src or root folders?

I'm currently facing an issue when trying to set a custom directory in Next JS. Although the default setup dictates that the pages directory should be located at the root or within the src directory, this arrangement doesn't fit my requirements ...

Tips on preventing the need for null or undefined checks in JS/Typescript singletons that have an initialization function

Is there a way to streamline the process of handling props in an Object literal that is dynamically initialized only once? I'm looking for a pattern that would eliminate the need for repetitive null/undefined checks and throw errors when certain metho ...

Highlight all text in the textbox upon selection

I discovered a helpful script on how to select all contents of a textbox when it receives focus using JavaScript or jQuery. My attempt to implement this in IE10 revealed that the focus was being cleared at a later time, and my efforts to prevent this (whi ...

What causes objects to intersect in A-Frame even when they are seemingly distanced from each other?

My charts appear in front of a globe object in A-Frame, but when I move the camera in the scene, the order of objects changes causing the charts to overlap and become invisible. I'm unsure of the cause and how to fix it. Here are some screenshots of t ...

The pagination feature for Swiper is experiencing issues across both desktop and mobile platforms

I am having an issue with the pagination display in the text. I would like it to appear below the text as a standalone item for better visibility. Another problem arises when viewing the page on mobile devices - the hamburger menu displays behind the slid ...

Retrieve the Json object or iterate through a loop

Looking for assistance with extracting values from a JSON object. Here's the JSON data I have: {"sockets":{"16":true},"length":1} I've attempted to retrieve the socket values using loops and other methods without success. Can anyone provide gui ...

Managing Empty Dates in JSON Parsing using GSON Library

Converting JSON to a custom bean is causing issues when the date value is null in the JSON. A valid JSON String, converts without any issue as both from and to dates have values: {"title":"1201 Box Title 1","fromdate":"01/02/2017","description":"1201 Box ...

Deleting a record in MongoDB based on a specific value present in a column

I am in search of more information about delete triggers in MongoDB. Source: Query on MongoDB Delete Triggers I am interested in converting DELETE operations to UPDATE + AUTOMATIC DELETE. To achieve this, I plan to introduce a new field called "flag" ...

What is the method for utilizing MongoDB to locate documents that correspond with a specific custom field?

I'm currently working on a task in node.js that involves searching for all documents matching a custom field. Below is the node.js code snippet: req.app.db.models.Property.find({ user: { id: req.params.id } }).exec(function(err, user) { if ...