Help with Data Format Keyed Array

I very well might be overthinking this, but I have a database query that outputs data like this…

What I need is to output the data like this…

options = {
  series: [
    {
      name: "09:32:00",
      data: [{
        x: 'QTY_250',
        y: 69
      }, {
        x: 'QTY_375',
        y: 151
      }, {
        x: 'QTY_500',
        y: 73
      }, {
        x: 'QTY_625',
        y: 29
...
      }]
    },
    {
      name: "09:33:00",
      data: [{
        x: 'QTY_250',
        y: 209
      }, {
        x: 'QTY_375',
        y: 302
      }, {
        x: 'QTY_500',
        y: 385
      }, {
        x: 'QTY_625',
        y: 357
...
      }]
    }
  ]
}

I was thinking I could use a keyed array but I can’t figure out how and I can’t find any information on using keyed arrays server side.

Any pointers would be most appreciated!

I usually just build it up “manually”

Start with a group and name it “options”

Inside options use a repeat named “series” and loop on the array “custom”

Then use set values inside the repeat for you various elements, like “name” set to “the_time”, etc.

“data” is another repeat inside series and loops the number of times need for all your qty sets.

Output on for all.

1 Like

Thanks Ken,

Got it working like this…

query_numbers is a record set that returns 9 records which is the number of qty rows I have. I use the $number of that to output the right value.

So this works - yay! It would be great to know if this could be done with a keyed array or maybe another more elegant solution.

1 Like

If you’re in control of the original data, then it’s all good, but if not you would want to evaluate the key name in your condition instead of relying on it always being in the same positional order.

One of the smart kids will have to chime in with something more elegant!

1 Like

I am in control of the data but that’s a good point, and another reason a more elegant solution would be better. I await the smart kids. :slight_smile:

Thanks again for your help!

1 Like

With the series key, I assume this is for charting. We have used RunJS to get the data in the format we want.
First we used custom query to write a query that could return as close of a structure as possible, and then use RunJS to get the final JSON data.

I don’t have an exact code to share with you, but nested loops should help you get the data structure you want.

Thanks. I’l check that out.