0

I'm trying to grab certain bits of data out of my JSON response to put in an array. This is my JSON response, and what I am interested in is the "counts" which is hourly, separated by either page "statement" or "logon".

{
   "report":{
      "type":"trended",
      "elements":[
         {
            "id":"page",
            "name":"Page"
         }
      ],
      "reportSuite":{
         "id":"retail",
         "name":"GlobPROD"
      },
      "period":"Wed.  3 Oct. 2018 - Fri.  5 Oct. 2018",
      "metrics":[
         {
            "id":"pageviews"
         }
      ],
      "segments":[
         {
            "id":"s13bb443734ab6a764639ff37",
            "name":"Information"
         }
      ],
      "data":[
         {
            "name":"Wed.  3 Oct. 2018",
            "year":2018,
            "month":10,
            "day":3,
            "hour":0,
            "breakdown":[
               {
                  "name":"CATEGORY:>Statement",
                  "url":"",
                  "counts":[
                     "242"
                  ]
               },
               {
                  "name":"CATEGORY:>Log On",
                  "url":"...CheckId.do",
                  "counts":[
                     "237"
                  ]
               }
            ],
            "breakdownTotal":[
               "2123"
            ]
         },
         {
            "name":"Wed.  3 Oct. 2018 (Hour 1)",
            "year":2018,
            "month":10,
            "day":3,
            "hour":1,
            "breakdown":[
               {
                  "name":"CATEGORY:>Statement",
                  "url":"",
                  "counts":[
                     "152"
                  ]
               },
               {
                  "name":"CATEGORY:>Log On",
                  "url":"",
                  "counts":[
                     "135"
                  ]
               }
            ],
            "breakdownTotal":[
               "1140"
            ]
         }

So what I am after is firstly to be able to split out the hourly data between statement and login, and secondly build an array of the data hour by hour.

logindata: [237, 135]

statementdata: [242, 152]

I've read that I can try and grab values using something a bit like this:

 var t = JSON.parse('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');

But how can I create a little loop to grab all the values I specify like in my example?

2
  • From where is 137 ? Commented Oct 8, 2018 at 12:21
  • @MihaiAlexandru-Ionut Sorry, it was 135 Commented Oct 8, 2018 at 12:22

2 Answers 2

2

You could use reduce method with forEach inside and build an object where each CATEGORY is separate property.

const data = {"report":{"type":"trended","elements":[{"id":"page","name":"Page"}],"reportSuite":{"id":"retail","name":"GlobPROD"},"period":"Wed.  3 Oct. 2018 - Fri.  5 Oct. 2018","metrics":[{"id":"pageviews"}],"segments":[{"id":"s13bb443734ab6a764639ff37","name":"Information"}],"data":[{"name":"Wed.  3 Oct. 2018","year":2018,"month":10,"day":3,"hour":0,"breakdown":[{"name":"CATEGORY:>Statement","url":"","counts":["242"]},{"name":"CATEGORY:>Log On","url":"...CheckId.do","counts":["237"]}],"breakdownTotal":["2123"]},{"name":"Wed.  3 Oct. 2018 (Hour 1)","year":2018,"month":10,"day":3,"hour":1,"breakdown":[{"name":"CATEGORY:>Statement","url":"","counts":["152"]},{"name":"CATEGORY:>Log On","url":"","counts":["135"]}],"breakdownTotal":["1140"]}]}}

const result = data.report.data.reduce((r, e) => {
  e.breakdown.forEach(el => {
    let key = el.name.split(':>')[1];
    if(!r[key]) r[key] = []
    r[key].push(...el.counts)
  })
  return r;
}, {})

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

I think (if the json structure is always the same) you could do it like this:

for( let item in json.report.data ){
  for( let subItem in item.breakdown ){
    if( subItem.name === "CATEGORY:>Statement" ){
      //do something with subItem.counts, the statement counts
    } else if( subItem.name === "CATEGORY:>Log On" ){
      //do something with subItem.counts, the logOn counts
    }
  }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.