Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading