Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - How to extract specific fields and values from a JSON fromat

I have a JSON output as follows which is coming from the response body

and I would like to pull only

  1. "contractId" and
  2. "contractStatus".

I'm using javascript to pull the data from the response body

{
  "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" : "",
    "amendmentReason" : "",
    "origin" : 0,
    "hierarchicalType" : "StandAlone",
    "expirationTermType" : "Fixed",
    "accessControl" : [ "Private To Team Members" ],
    "relatedId" : "NA",
    "affectedParties" : [ {
      "name" : "[CM] KIER GROUP",
      "systemID" : "50066293",
      "smVendorID" : null,
      "organizationIDs" : [ {
        "domain" : "buyersystemid",
        "value" : "50066293"
      } ],
      "address" : {
        "name" : "50066293",
        "uniqueName" : "50066293",
        "phone" : "",
        "fax" : "",
        "city" : "",
        "state" : "",
        "postalCode" : ""
      }
    } ],
    "maximumNumberOfRenewals" : 0,
    "autoRenewalInterval" : 0,
    "isTestProject" : true,
    "customFields" : [ {
      "fieldId" : "cus_ThirdPartyUpliftsDetails",
      "fieldName" : "Third Party Uplifts Details",
      "fieldDataType" : "multiLineTextValue",
      "booleanValue" : null,
      "dateValue" : null,
      "bigDecimalValue" : null,
      "intValue" : null,
      "moneyValue" : null,
      "textValue" : null,
      "multiLineTextValue" : [ ],
      "textMultiSelectValue" : null,
      "percentValue" : null,
      "urlValue" : null,
      "userMultiSelectValue" : null,
      "uomValue" : null,
      "regionValue" : null,
      "productValue" : null,
      "addressValue" : null,
      "commodityValue" : null,
      "countryValue" : null,
      "currencyValue" : null,
      "departmentValue" : null,
      "flexMasterDataFieldClassName" : null,
      "flexMasterDataTypeValue" : null,
      "uniqueName" : null,
      "subContent" : null,
      "templateProperties" : [ "TextArea1000Field", "FullAmendmentEditField", "ForceRefreshField", "ZoneRightField" ],
      "enumerationValues" : null,
      "visibilityConstraint" : null,
      "editabilityConstraint" : null,
      "validityConstraint" : null,
      "instanceOfSubContent" : {
        "flexMasterDataFields" : null
      },
      "customField" : true
    }, {
      "fieldId" : "cus_PreviousBuyer",
      "fieldName" : "Previous Buyer",
      "fieldDataType" : "textValue",
      "booleanValue" : null,
      "dateValue" : null,
      "bigDecimalValue" : null,
      "intValue" : null,
      "moneyValue" : null,
      "textValue" : [ null ],
      "multiLineTextValue" : null,
      "textMultiSelectValue" : null,
      "percentValue" : null,
      "urlValue" : null,
      "userMultiSelectValue" : null,
      "uomValue" : null,
      "regionValue" : null,
      "productValue" : null,
      "addressValue" : null,
      "commodityValue" : null,
      "countryValue" : null,
      "currencyValue" : null,
      "departmentValue" : null,
      "flexMasterDataFieldClassName" : null,
      "flexMasterDataTypeValue" : null,
      "uniqueName" : null,
      "subContent" : null,
      "templateProperties" : [ "String1000Field" ],
      "enumerationValues" : null,
      "visibilityConstraint" : null,
      "editabilityConstraint" : null,
      "validityConstraint" : null,
      "instanceOfSubContent" : {
        "flexMasterDataFields" : null
      },
      "customField" : true

    }, {
      "fieldId" : "cus_GroupTier",
      "fieldName" : "Group Tier",
      "fieldDataType" : "textValue",
      "booleanValue" : null,
      "dateValue" : null,
      "bigDecimalValue" : null,
      "intValue" : null,
      "moneyValue" : null,
      "textValue" : [ null ],
      "multiLineTextValue" : null,
      "textMultiSelectValue" : null,
      "percentValue" : null,
      "urlValue" : null,
      "userMultiSelectValue" : null,
      "uomValue" : null,
      "regionValue" : null,
      "productValue" : null,
      "addressValue" : null,
      "commodityValue" : null,
      "countryValue" : null,
      "currencyValue" : null,
      "departmentValue" : null,
      "flexMasterDataFieldClassName" : null,
      "flexMasterDataTypeValue" : null,
      "uniqueName" : null,
      "subContent" : null,
      "templateProperties" : [ "String1000Field", "PickListField", "FullAmendmentEditField", "AdministrativeEditField", "TerminationEditField" ],
      "enumerationValues" : null,
      "visibilityConstraint" : null,
      "editabilityConstraint" : null,
      "validityConstraint" : null,
      "instanceOfSubContent" : {
        "flexMasterDataFields" : null
      },
      "customField" : true
 
    }, {
      "fieldId" : "cus_SuboptimalTCs",
      "fieldName" : "Suboptimal T&Cs",
      "fieldDataType" : "booleanValue",
      "booleanValue" : null,
      "dateValue" : null,
      "bigDecimalValue" : null,
      "intValue" : null,
      "moneyValue" : null,
      "textValue" : null,
      "multiLineTextValue" : null,
      "textMultiSelectValue" : null,
      "percentValue" : null,
      "urlValue" : null,
      "userMultiSelectValue" : null,
      "uomValue" : null,
      "regionValue" : null,
      "productValue" : null,
      "addressValue" : null,
      "commodityValue" : null,
      "countryValue" : null,
      "currencyValue" : null,
      "departmentValue" : null,
      "flexMasterDataFieldClassName" : null,
      "flexMasterDataTypeValue" : null,
      "uniqueName" : null,
      "subContent" : null,
      "templateProperties" : [ "BooleanField", "FullAmendmentEditField", "ZoneRightField" ],
      "enumerationValues" : null,
      "visibilityConstraint" : null,
      "editabilityConstraint" : null,
      "validityConstraint" : null,
      "instanceOfSubContent" : {
        "flexMasterDataFields" : null
      },
      "customField" : true
    } ]
  } ],
  "count" : null,
  "start" : null,
  "warnings" : null
}
question from:https://stackoverflow.com/questions/65917952/how-to-extract-specific-fields-and-values-from-a-json-fromat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

const response = {
  "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": "",
    "amendmentReason": "",
    "origin": 0,
    "hierarchicalType": "StandAlone",
    "expirationTermType": "Fixed",
    "accessControl": ["Private To Team Members"],
    "relatedId": "NA",
    "affectedParties": [{
      "name": "[CM] KIER GROUP",
      "systemID": "50066293",
      "smVendorID": null,
      "organizationIDs": [{
        "domain": "buyersystemid",
        "value": "50066293"
      }],
      "address": {
        "name": "50066293",
        "uniqueName": "50066293",
        "phone": "",
        "fax": "",
        "city": "",
        "state": "",
        "postalCode": ""
      }
    }],
    "maximumNumberOfRenewals": 0,
    "autoRenewalInterval": 0,
    "isTestProject": true,
    "customFields": [{
      "fieldId": "cus_ThirdPartyUpliftsDetails",
      "fieldName": "Third Party Uplifts Details",
      "fieldDataType": "multiLineTextValue",
      "booleanValue": null,
      "dateValue": null,
      "bigDecimalValue": null,
      "intValue": null,
      "moneyValue": null,
      "textValue": null,
      "multiLineTextValue": [],
      "textMultiSelectValue": null,
      "percentValue": null,
      "urlValue": null,
      "userMultiSelectValue": null,
      "uomValue": null,
      "regionValue": null,
      "productValue": null,
      "addressValue": null,
      "commodityValue": null,
      "countryValue": null,
      "currencyValue": null,
      "departmentValue": null,
      "flexMasterDataFieldClassName": null,
      "flexMasterDataTypeValue": null,
      "uniqueName": null,
      "subContent": null,
      "templateProperties": ["TextArea1000Field", "FullAmendmentEditField", "ForceRefreshField", "ZoneRightField"],
      "enumerationValues": null,
      "visibilityConstraint": null,
      "editabilityConstraint": null,
      "validityConstraint": null,
      "instanceOfSubContent": {
        "flexMasterDataFields": null
      },
      "customField": true
    }, {
      "fieldId": "cus_PreviousBuyer",
      "fieldName": "Previous Buyer",
      "fieldDataType": "textValue",
      "booleanValue": null,
      "dateValue": null,
      "bigDecimalValue": null,
      "intValue": null,
      "moneyValue": null,
      "textValue": [null],
      "multiLineTextValue": null,
      "textMultiSelectValue": null,
      "percentValue": null,
      "urlValue": null,
      "userMultiSelectValue": null,
      "uomValue": null,
      "regionValue": null,
      "productValue": null,
      "addressValue": null,
      "commodityValue": null,
      "countryValue": null,
      "currencyValue": null,
      "departmentValue": null,
      "flexMasterDataFieldClassName": null,
      "flexMasterDataTypeValue": null,
      "uniqueName": null,
      "subContent": null,
      "templateProperties": ["String1000Field"],
      "enumerationValues": null,
      "visibilityConstraint": null,
      "editabilityConstraint": null,
      "validityConstraint": null,
      "instanceOfSubContent": {
        "flexMasterDataFields": null
      },
      "customField": true

    }, {
      "fieldId": "cus_GroupTier",
      "fieldName": "Group Tier",
      "fieldDataType": "textValue",
      "booleanValue": null,
      "dateValue": null,
      "bigDecimalValue": null,
      "intValue": null,
      "moneyValue": null,
      "textValue": [null],
      "multiLineTextValue": null,
      "textMultiSelectValue": null,
      "percentValue": null,
      "urlValue": null,
      "userMultiSelectValue": null,
      "uomValue": null,
      "regionValue": null,
      "productValue": null,
      "addressValue": null,
      "commodityValue": null,
      "countryValue": null,
      "currencyValue": null,
      "departmentValue": null,
      "flexMasterDataFieldClassName": null,
      "flexMasterDataTypeValue": null,
      "uniqueName": null,
      "subContent": null,
      "templateProperties": ["String1000Field", "PickListField", "FullAmendmentEditField", "AdministrativeEditField", "TerminationEditField"],
      "enumerationValues": null,
      "visibilityConstraint": null,
      "editabilityConstraint": null,
      "validityConstraint": null,
      "instanceOfSubContent": {
        "flexMasterDataFields": null
      },
      "customField": true

    }, {
      "fieldId": "cus_SuboptimalTCs",
      "fieldName": "Suboptimal T&Cs",
      "fieldDataType": "booleanValue",
      "booleanValue": null,
      "dateValue": null,
      "bigDecimalValue": null,
      "intValue": null,
      "moneyValue": null,
      "textValue": null,
      "multiLineTextValue": null,
      "textMultiSelectValue": null,
      "percentValue": null,
      "urlValue": null,
      "userMultiSelectValue": null,
      "uomValue": null,
      "regionValue": null,
      "productValue": null,
      "addressValue": null,
      "commodityValue": null,
      "countryValue": null,
      "currencyValue": null,
      "departmentValue": null,
      "flexMasterDataFieldClassName": null,
      "flexMasterDataTypeValue": null,
      "uniqueName": null,
      "subContent": null,
      "templateProperties": ["BooleanField", "FullAmendmentEditField", "ZoneRightField"],
      "enumerationValues": null,
      "visibilityConstraint": null,
      "editabilityConstraint": null,
      "validityConstraint": null,
      "instanceOfSubContent": {
        "flexMasterDataFields": null
      },
      "customField": true
    }]
  }],
  "count": null,
  "start": null,
  "warnings": null
}


const new_object = response.value.map(({ contractId, contractStatus }) => {
  return { contractId, contractStatus };
});

console.log(new_object);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...