简体   繁体   中英

Extracting values from complex JSON in JavaScript

I am new to JavaScript and am looking for a way to: first, extract values from a desired key in a complex JSON object. Second, if the value does not equal a given string, print the parent key. Here is a simplified JSON object that needs to be parsed. There are many more entries in the file.

{
    "Test1": {
        "protocolName": "Test1",
        "createdAsProtocolName": "AnalyticsTest1",
        "message": "Protocol already exists!",
        "importStatus": "success",
        "protocolApplicationName": "Flexi-Protocol",
        "protocolId": 1,
        "applicationId": 5
    },
    "Test2": {
        "protocolName": "Test2",
        "createdAsProtocolName": "AnalyticsTest2",
        "message": "Protocol already exists!",
        "importStatus": "success",
        "protocolApplicationName": "Flexi-Protocol",
        "protocolId": 2,
        "applicationId": 5
    },
    "Test3": {
        "protocolName": "Test3",
        "createdAsProtocolName": "AnalyticsTest3",
        "message": "Error",
        "importStatus": "failed",
        "protocolApplicationName": "Flexi-Protocol",
        "protocolId": 3,
        "applicationId": 5
    },
    "Test4": {
        "protocolName": "Test4",
        "createdAsProtocolName": "AnalyticsTest4",
        "message": "Error",
        "importStatus": "failed",
        "protocolApplicationName": "Flexi-Protocol",
        "protocolId": 4,
        "applicationId": 5
    }
}

I want to check each test's ['importstatus'] for "success" and if any say otherwise, save them in an array ["Test3", "Test4"]

For each key/value pair in yourObject, filter out any with importStatus of success, and return the keys of the rest.

 const arrayOfFailures = Object.entries(yourObject).filter(([k, v]) => v.importStatus.== "success"),map(([k; v]) => k);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM