-1

I have a JSON format with this format:

var myJSON = 
{
  [
    {"project":"4","value":"5"},
    {"project":"2","value":"3"},
    {"project":"4","value":"5"},
    {"project":"4","value":"4"},
    {"project":"1","value":"8"},
  ]
}

I would like to do a query like SELECT SUM(value) WHERE Project = 4. How can I do such a query in Javascript? I checked with JSON Path and I couldn't find a way to make such query.

0

5 Answers 5

0

Your json data doesn't look right. Probably you need to change it like

var myJSON = 
{
  data: [
    {"project":"4","value":"5"},
    {"project":"2","value":"3"},
    {"project":"4","value":"5"},
    {"project":"4","value":"4"},
    {"project":"1","value":"8"},
  ]
}

And then sum will look like:

var myJSON = 
{
  data: [
    {"project":"4","value":"5"},
    {"project":"2","value":"3"},
    {"project":"4","value":"5"},
    {"project":"4","value":"4"},
    {"project":"1","value":"8"},
  ]
}

var sum = 0;
for(var i = 0; i< myJSON.data.length; i++)  {
  var project = myJSON.data[i];
  if (project.project === "4"){
    sum += parseInt(project.value, 10);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

A simple for with a condition do the work :

var sum = 0;
for(var i in myJSON) { 
    if(myJSON[i].project == 4)
        sum += parseInt(myJSON[i].value);
}

Comments

0

var JSON = {
  data: [
    {"project":"4","value":"5"},
    {"project":"2","value":"3"},
    {"project":"4","value":"5"},
    {"project":"4","value":"4"},
    {"project":"1","value":"8"},
  ]
}

var sum = 0;
JSON.data.forEach(function(val){
    sum += val.project==="4"? 
            parseInt(val.value, 10): 0;
});
    
console.log(sum);

Comments

0
var sum = jsonPath(myjsonobj, "$.data[?(@.project=='4')].value").reduce(function(pv, cv){
   return parseInt(cv) + parseInt(pv);
});

Some explanations:

$.data[?(@.project=='4')].value- gives an array with only the values where project == "4"

reduce(...) - executes a function for all elements of an array (see ECMAScript doc)

pv, cv - previous and current value of function invocation for array elements. Previous value is (in this solution) the sum so far, current value is the current array element (as string, as it is in your data). So, for each array element, its integer value is added to the "previous" value and, in the end, returned by reduce().

Comments

0

This is not a valid json, correct it first. If you json is like this:

var myJSON = 
{
  "data":[
    {"project":"4","value":"5"},
    {"project":"2","value":"3"},
    {"project":"4","value":"5"},
    {"project":"4","value":"4"},
    {"project":"1","value":"8"}
  ]
}

Then Use this function to get the sum :

getSum(key){
    let sum = 0;
    myJSON.data.forEach(i=>{
       if(i.project == key){
          sum+= parseFloat(i.value);
       }
    })
  return sum;
}

2 Comments

That is no valid JSON either :-) Put data in double quotes, and remove last comma...
Nice :), now it is proper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.