Transpose the Object/Array

Hi,
I have an output from mysql query in this format,

[{"R":"Batch Preparation Delay","D":281},{"R":"Tea - Lunch Break","D":109},{"R":"Laminate Loading","D":58}]

i want the output to be changed to this format

[{"Batch Preparation Delay"},{"Tea - Lunch Break"},{"Laminate Loading"}]
[{281},{109},{58}]

How do i do that ? i searched the forum and there was a suggestion to use map function in function node, but there was no clear instructions, i don't know javascript so please someone can help me....

Not possible unless it it a string.

That is not valid JS or JSON.

The output from mysql is an array of objects. Each object has name : value pairs.

The format you say you want is invalid because it doesn't have name:value pairs, just values.

So do you want arrays
[281, 109, 58]
Or arrays of objects?
[{D:281}, {D:109},{D:58}]

i want to feed the output to a timeplot graph in flexdash, and i think it requires the format to be in
below format, i am not sure what this format is, since i dont speak this 'language'

image

i think it is the one without the curly braces...

[["Batch Preparation Delay","Tea - Lunch Break","Laminate Loading"}]]
[[281],[109],[58]]

Try setting msg.labels to JSONata J: [$$.payload.R]
and then msg.payload to JSONata J: [[$$.payload.D]] or [$$.payload.D.[$]] in a change node.
You have to set msg.labels first, as if you set payload the data will be overwrtten.

1 Like

I don't know about charts in Flexdash.

Since I am also a bit uncertain how to use map() I've written a function using your data to explain it to myself. It creates arrays and for comparison, an object. I hope it's useful.

const source=[{ "R": "Batch Preparation Delay", "D": 281 }, { "R": "Tea - Lunch Break", "D": 109 }, { "R": "Laminate Loading", "D": 58 }]

// The map() function calls another function for each element of an array.
// It returns another array

// Two different ways to use map() to transform the input array
// Firstly Define a function to return one array element D value
function getDValues(element) {
    return element.D
}
// Call that function for each array element
const Darr = source.map(getDValues)

//Exactly the same thing for R values but expressed more compactly
const Rarr = source.map(e => { return e.R })

// The compact form for D values
//const Darr = source.map(e => { return e.D })

// to create an array of objects
const Robj = source.map(e => { return { R: e.R } })

msg.payload = {R:Rarr, D:Darr, Robj: Robj}
return msg;

ps You can always rely on @E1cid to provide a Jsonata answer, sadly I can never grasp why it works! :smiley:

1 Like

Silicon Pixies.

[edit] They run all over payload, mapping and returning all elements with property D in an array.

Perfect !. I got the required format. now I will feed that into the chart and see, how it works.

I also got the same result by modifying my mysql query, got two separate queries to output the results into two different arrays and then merged them with a join node, and got the same result as your method

thanks !

Will give this a try @E1cid 's solution worked for me. and i also got a workaround by having two separate queries and then merging them with join node. (not a neat solution, but works)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.