简体   繁体   中英

Return only specif element of JSON

I'm parsing some information using JSON

And after calling Json.projectStatus.status I receive one of two responses : ( ERROR or OK )

And I want only to display ERROR status :

function search(projectKey, projectName, lastAnalysis) {
    var url = "https://***?projectKey=" + projectKey;
    request(
        {
            url : url,
            headers : {
                "Authorization" : auth
            }
        },
        function (error, response, body) {
            if(error){
                console.log(error);
            } else {
                process(body, projectName, lastAnalysis);
            }
        }
    );
}

function process(response, projectName, lastAnalysis){
    var Json = JSON.parse(response);
    var color = "#6f6f68";
    var description = "";
    if (Json.projectStatus.status == "ERROR") {
        color = "#FA4643";
        description = ", Errors: ";
        var Data = Json.projectStatus.conditions.map(function(status) {

            if (status.actualValue > 0) {
                description = description 
                            + status.actualValue 
                            + " " + status.metricKey 
                            + ", ";
            }
        });
    }
    var project = {
        name: projectName, 
        status: Json.projectStatus.status,    // Filter only "error"
        color:color, 
        lastAnalysis:lastAnalysis, 
        description
    };
    Projects.push(project);
    counter++;
    if (counter == numberProjects) {
        parse();
    }
}

JSON response (example)

{"projectStatus":{"status":"ERROR","conditions":..}}
{"projectStatus":{"status":"OK","conditions":..}}
{"projectStatus":{"status":"ERROR","conditions"...}}
{"projectStatus":{"status":"ERROR","conditions":..}}
{"projectStatus":{"status":"OK","conditions":..}}

Actual result (example):

  • ERROR
  • OK
  • ERROR
  • ERROR
  • OK

expected result (example):

  • ERROR
  • ERROR
  • ERROR
if(Json.projectStatus.status == "ERROR"){
  Projects.push(project);
}

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