Convert csv format to JSON format

Hi I am getting a MQTT data in csv data format 1,1.4439263,103.7842,1575539820. Basically its a GPS data in the following format (#, lat, lng, timestamp) I need to create a JSON file in this manner.
[ 1, {
"lat" : 1.4439263,
"lng" : 103.7842,
"timestamp" : 1575539820
} ]
I am not sure on how to do this.

You can start with the CSV node to do the initial parsing of the data, and then a Function node to rearrange it into the structure you want.

Start with that and see how you get on.

Hi Knolleary,

I am able to split the values using "split" and to " to number converter" nodes. i will try to see how can I combine all the 4 values into the JSON format shown using the function node.

If you are using the split node then you are making things harder for yourself as you now have your values spread across multiple messages. The CSV node will parse the data and keep it all in the one message.

also - the format for what you said you wanted isn't valid JSON - so you need to correct that first or you will struggle. If you just wanted

[ {
"name": "1",
"lat" : 1.4439263,
"lng" : 103.7842,
"timestamp" : 1575539820
} ]

would make more sense and a csv node with a template of name,lat,lng,timestamp and set to output an array should just do it.

(Of course if you used lon instead of lng - then you could feed the whole array into a node-red-contrib-web-worldmap node to display them all :slight_smile:

Hi,
Yes i'm struggling to comprehend how to add keys to the parsed messages. That is the reason Im trying to split them up.

Hmm, actually i created a firebase database structure and exported the JSON file, and that's how it was formatted below. So I believe, I would need to create a similar format to store the values.
image

[ null, {
"lat" : 1.4439263,
"lng" : 103.7842,
"timestamp" : 1575539820
} ]