Java Script Object to Json array


I need to convert Java Script Object to Json Array.

Can any one help me. I am new to java scripting.

Thanks in advance

Could you post your flow ?
Select all the nodes, right top hamburger menu -> export.

use the </> icon here in the editor before pasting it.

Objects and Arrays are very different types of structure.

At the moment you have an object that looks like:

msg.payload =  {
   Network: {
      value1: "31",
      value2: "45"

What do you want the array to look like? Do you want:

msg.payload =  [ "31", "45" ];

Or do you want value1, value2 and Network in there somewhere?

I want return as,
msg.payload= array which will hold Network value1=31 and value2=45

[{"id":"60f7cc3e.df5c04","type":"inject","z":"9f64ee38.cd9a1","name":"tempdata","topic":"","payload":"{\"Network\":{\"value1\":\"31\",\"value2\":\"45\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":1100,"wires":[["a846eda6.9fbc6"]]},{"id":"a846eda6.9fbc6","type":"json","z":"9f64ee38.cd9a1","name":"","property":"payload","action":"obj","pretty":true,"x":310,"y":1160,"wires":[["37cb8ede.15b722","78cc22f3.07a3dc"]]},{"id":"37cb8ede.15b722","type":"function","z":"9f64ee38.cd9a1","name":"testing","func":"var tempdata = msg.payload;\n//var obj= Object.values(tempdata)\nconsole.log(Object.values(tempdata));\n","outputs":1,"noerr":0,"x":370,"y":1260,"wires":[["dbe474fa.8b5458"]]}]

That's doesn't really make it much clearer.

Do you mean something like: [ "31", "45"] or something else? Are you able to provide an actual example of the JSON you'd like to get?

Can you explain why you want this as an array? That may help us understand what format you want.

Actual Json Format: {"Network":{"value":"31","time":"31-03-2020#12:26:45"}}

Need to format the above data to this format as below:

This is the conversion I need to achieve
After the first part is over, I will try to parse the time and date part.
I think now, my objective is clear

Take a look at JSONata:
It is a query and transformation language available for use in the change node. You can use it to change one object into another following rules you define in that language. It sounds like the most useful one for your situation, as the objects don't even look alike in the slightest.

Your flow example shows:


What should the output become ?

{"d":[{"tag":"Network","value":31}] - where is value 2 ?

Do you want:

{"d":[{"tag":"Network","value":31},{"tag":"Network","value":45}]} ?

Yes. This is what I want.
Can you tell me the method to get this.

{"d":[{"tag":"Network","value":31},{"tag":"Network","value":45}]} .......Yes

One more question:

Is your "testdata" actual data ? ie. "value1", "value2" - or is it "value" ?

it is only "value" not "value1" or "value2"

But then your "testdata" example is not correct.


Could you give example data that matches reality ?

Pls do not consider testdata example.
Real Data Packet:
Array Type packet to which above needs to get converted:

the main packet is just a network signal packet with date and time stamp.
Is this ok now?

If you receive 1 json string with 1 timestamp, how do you create an array with 3 timestamp values ?
Provide more data that we can work with.

I just want to parse the main json string in to an array of three objects.
What type of data you want?

Real Data Packet:
We can use array for example

is this info ok?

Yes you are right.

Basic idea.

[{"id":"c15f3c09.ccbb58","type":"inject","z":"8a79ddd6.20ebf","name":"tempdata","topic":"","payload":"{\"Network\":{\"value\":\"28\",\"time\":\"31-03-2020#21:26:45\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":255,"y":288,"wires":[["a38bf69e.e68bb"]],"l":false},{"id":"a38bf69e.e68bb","type":"json","z":"8a79ddd6.20ebf","name":"","property":"payload","action":"obj","pretty":true,"x":303,"y":288,"wires":[["fa5735c7.dc066"]],"l":false},{"id":"fa5735c7.dc066","type":"function","z":"8a79ddd6.20ebf","name":"","func":"value = parseInt(msg.payload.Network.value)\ntime = msg.payload.Network.time\n\nreturn {payload:{d:[{tag:\"Network\",value:value}],ts:time }}","outputs":1,"noerr":0,"x":351,"y":288,"wires":[["e5dbd547.95d73"]],"l":false},{"id":"e5dbd547.95d73","type":"debug","z":"8a79ddd6.20ebf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":434,"y":288,"wires":[]}]

Timestamp conversion is a different ballgame.

Ok. Thanks for the solution.