Rest Api - Javascript array

Hi,
I need your support to get sonar rest api result. I read the community forums but i couldn’t find the solution. I think i need to custom javascript to read project status conditions array list but i am not a developer - could you help to javascript part.

{
  "projectStatus": {
    "status": "OK",
    "conditions": [
      {
        "status": "OK",
        "metricKey": "new_reliability_rating",
        "comparator": "GT",
        "periodIndex": 1,
        "errorThreshold": "1",
        "actualValue": "1"
      },
      {
        "status": "OK",
        "metricKey": "new_security_rating",
        "comparator": "GT",
        "periodIndex": 1,
        "errorThreshold": "1",
        "actualValue": "1"
      },
      {
        "status": "OK",
        "metricKey": "new_maintainability_rating",
        "comparator": "GT",
        "periodIndex": 1,
        "errorThreshold": "1",
        "actualValue": "1"
      },
      {
        "status": "OK",
        "metricKey": "new_coverage",
        "comparator": "LT",
        "periodIndex": 1,
        "errorThreshold": "80",
        "actualValue": "0.0"
      },
      {
        "status": "OK",
        "metricKey": "new_duplicated_lines_density",
        "comparator": "GT",
        "periodIndex": 1,
        "errorThreshold": "3",
        "actualValue": "0.0"
      }
    ],
    "periods": [
      {
        "index": 1,
        "mode": "PREVIOUS_VERSION",
        "date": "2022-02-05T00:26:02+0000"
      }
    ],
    "ignoredConditions": true,
    "period": {
      "mode": "PREVIOUS_VERSION",
      "date": "2022-02-05T00:26:02+0000"
    }
  }
}

Hi @sefa.gerdan ,

A warm welcome to the eazyBI community!

JavaScript is pretty common knowledge. For example, a colleague of yours could know it enough to help you access the elements in the “conditions” array. You can also see some examples on our documentation page - Import from REST API.

When importing data from a custom data source, the first question should be: How do you want to build the report? From there, you can determine how you import the data.

So, how do you want to retrieve the values from the “conditions” array? If you want each metric to be its own column and have the status value, you can try the JavaScript code below:

if (doc.projectStatus.conditions && doc.projectStatus.conditions.length > 0) {
  for (i=0;i<doc.projectStatus.conditions.length;i++) {
    if (doc.projectStatus.conditions[i].metricKey == "new_reliability_rating") {
      doc.reliabilityRating = doc.projectStatus.conditions[i].status;
    } else if (doc.projectStatus.conditions[i].metricKey == "new_security_rating") {
      doc.securityRating = doc.projectStatus.conditions[i].status;
    }  // add the rest of conditions in a similar way
  }
}

The JavaScript code above will create a column only for the first two “conditions” array elements. Try to update it on your own to work for the complete list of conditions or share more details about how you want to import the data.

Best,
Roberts // support@eazybi.com

thank you so much @roberts.cacus
i enrich the jscript file like that but when I insert the data , I don’t edit anything - after I click the insert button on the right and try to create some report dimensions or property i don’t see anything.

if (doc.projectStatus.conditions && doc.projectStatus.conditions.length > 0) {
  for (i=0;i<doc.projectStatus.conditions.length;i++) {
    if (doc.projectStatus.conditions[i].metricKey == "new_reliability_rating") {
      doc.reliabilityRating = doc.projectStatus.conditions[i].status;
    } else if (doc.projectStatus.conditions[i].metricKey == "new_security_rating") {
      doc.securityRating = doc.projectStatus.conditions[i].status;
    } else if (doc.projectStatus.conditions[i].metricKey == "new_maintainability_rating") {
      doc.maintainabilityRating = doc.projectStatus.conditions[i].status;
    } else if (doc.projectStatus.conditions[i].metricKey == "new_coverage") {
      doc.coverageRating = doc.projectStatus.conditions[i].status;
    } else if (doc.projectStatus.conditions[i].metricKey == "new_duplicated_lines_density") {
      doc.duplicated_linesDensity = doc.projectStatus.conditions[i].status;
    } 
  }
}


Hi @sefa.gerdan,

That is because you have selected only the “projecStatus period date” column for import as the Time dimension. Please share more details about the report requirement. After that, I can share suggestions for the data mapping.

In the meantime, please visit our documentation page regarding data mapping - Data mapping.

Best,
Roberts // support@eazybi.com

Hi @roberts.cacus
thank you so much. I found the problem,
delete data :slight_smile: after that edit field was enabled

1 Like