0

i have a json file which goes like below

[{
    "data": {
        "aggregations": {
            "openissue": {
                "buckets": [{
                        "doc_count": 5,
                        "date": "25 aug"
                    },
                    {
                        "doc_count": 6,
                        "date": "26 aug"
                    },
                    {
                        "doc_count": 7,
                        "date": "27 aug"
                    },
                    {
                        "doc_count": 8,
                        "date": "28 aug"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
},
{
    "data": {
        "aggregations": {
            "closeissue": {
                "buckets": [{
                        "doc_count": 9,
                        "date": "29 aug"
                    },
                    {
                        "doc_count": 10,
                        "date": "30 aug"
                    },
                    {
                        "doc_count": 11,
                        "date": "31 aug"
                    },
                    {
                        "doc_count": 12,
                        "date": "1 sept"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
}, {
    "data": {
        "aggregations": {
            "pendingissue": {
                "buckets": [{
                        "doc_count": 13,
                        "date": "10 sept"
                    },
                    {
                        "doc_count": 14,
                        "date": "13 sept"
                    },
                    {
                        "doc_count": 15,
                        "date": "18 sept"
                    },
                    {
                        "doc_count": 16,
                        "date": "19 sept"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
}

]

the Number of objects are dynamic which i am getting by ajax call,This json is created after the ajax call.I am trying to filter out and the json to a desired format where the number of object depends on the number of date.

the Desired output should be something like

    [
  {
    "Key_as_string": "25aug",
    "values": [
      {
        "label": "open-issues",
        "doc_count": 10
      },
      {
        "label": "closed-issues",
        "doc_count": 10
      },
      {
        "label": "exempted-issues",
        "doc_count": 10
      }
    ]
  },
  {
    "Key_as_string": "26aug",
    "values": [
      {
        "label": "open-issues",
        "doc_count": 10
      },
      {
        "label": "closed-issues",
        "doc_count": 10
      },
      {
        "label": "pending-issues",
        "doc_count": 10
      }
    ]
  }
]

How do i do it using javascript or jquery

1 Answers1

0

The idea is to flatten all issues into a single array and then group by date, finally construct it to the required format.

const payload = [
{
    "data": {
        "aggregations": {
            "openissue": {
                "buckets": [{
                        "doc_count": 5,
                        "date": "25 aug"
                    },
                    {
                        "doc_count": 6,
                        "date": "26 aug"
                    },
                    {
                        "doc_count": 7,
                        "date": "27 aug"
                    },
                    {
                        "doc_count": 8,
                        "date": "28 aug"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
},
{
    "data": {
        "aggregations": {
            "closeissue": {
                "buckets": [{
                        "doc_count": 9,
                        "date": "29 aug"
                    },
                    {
                        "doc_count": 10,
                        "date": "30 aug"
                    },
                    {
                        "doc_count": 11,
                        "date": "31 aug"
                    },
                    {
                        "doc_count": 12,
                        "date": "1 sept"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
}, {
    "data": {
        "aggregations": {
            "pendingissue": {
                "buckets": [{
                        "doc_count": 13,
                        "date": "10 sept"
                    },
                    {
                        "doc_count": 14,
                        "date": "13 sept"
                    },
                    {
                        "doc_count": 15,
                        "date": "18 sept"
                    },
                    {
                        "doc_count": 16,
                        "date": "19 sept"
                    }
                ]
            }
        }

    },
    "a": "not needed",
    "b": "not needed"
}
]
//
let flat = []
payload.forEach(item => {
 const preIssueType = Object.keys(item.data.aggregations)[0]
  item.data.aggregations[preIssueType].buckets.forEach(issue => {
   flat.push({label: preIssueType, doc_count: issue.doc_count, date: issue.date})   
  })
})
const grouped = _.groupBy(flat, 'date')
let result = []
for(const key in grouped){
 result.push({'Key_as_string': key, values: grouped[key]})
}

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

forgot to say, lodash is not compulsory but will save you some coding effort.

spiritwalker
  • 2,257
  • 14
  • 9
  • Thank you So much for replying. I am not familiar with loadsh. So i will try with plain javascript.In That case the code doesn't change much right?Sorry,I am very new in javascript . – Trinanjan Saha Sep 14 '17 at 02:45
  • @TrinanjanSaha _.groupBy is the only place where lodash is used. So if you want to use native javascript, there is post of how to do it in pure javascript. https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects. – spiritwalker Sep 14 '17 at 02:52
  • i am trying as you mentioned.I am getting some error.this is the fiddle attached http://jsfiddle.net/y45Lfupw/62/ – Trinanjan Saha Sep 14 '17 at 03:02
  • you need to declare groupby function before you can use it. So move the declaration to be in front of groupby() call. Or just simply move the groupby declaration to the top :) – spiritwalker Sep 14 '17 at 03:05
  • can i change the output object structure in such a way so that,for every particular date if there is no openrequest/closerequest(basically if no label is present) , it should add empty doc count with the label correspondingly.The sample output is attached in the js fiddle http://jsfiddle.net/y45Lfupw/75/ . @spiritwalker – Trinanjan Saha Sep 15 '17 at 01:38
  • it shouldn't be too hard to achieve that. I can imagine that you need to add 3 steps to existing code for this purpose. 1. sort the result on "Key_as_string", if your server api has done the ordering, you can skip this step; 2. compare every two nearby elements in the result array and check how many days apart; 3. create a number of objects(the number based on the days apart) with empty count and push them to the result array. I hope it makes sense to you. – spiritwalker Sep 15 '17 at 01:45
  • I am not able to understand how does the date difference matter here?My output should be like for any date i have to have all key present even if there are no data,so i have to add o to count..so it'll be like "25 aug " openissue= 0,closeissue=0(this value i am pushing because it's not coming from api),pendingissue=0(this value i am pushing because it's not coming from api) jsfiddle.net/y45Lfupw/75 . – Trinanjan Saha Sep 15 '17 at 01:56
  • @TrinanjanSaha I misunderstood your question at the first place, I put up a jsfiddle, go check if this is what you need. http://jsfiddle.net/spiritwalker/y45Lfupw/76/ – spiritwalker Sep 15 '17 at 02:46
  • Thanks a lot.My data is dynamic.so i just have to store keys in array issue_type instead of hardcoding it? – Trinanjan Saha Sep 15 '17 at 03:01