# Convert csv format to JSON format

**URL:** <https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810>\
**Category:** General\
**Created:** [5 December 2019 05:27 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810 "2019-12-05T05:27:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajendran20](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rajendran20/32/14706_2.png) [@rajendran20](https://discourse.nodered.org/u/rajendran20)\
**Post date:** [5 December 2019 05:27 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/1 "2019-12-05T05:27:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 December 2019 07:24 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/2 "2019-12-05T07:24:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rajendran20](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rajendran20/32/14706_2.png) [@rajendran20](https://discourse.nodered.org/u/rajendran20)\
**Post date:** [5 December 2019 08:19 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/3 "2019-12-05T08:19:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 December 2019 08:39 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/4 "2019-12-05T08:39:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 December 2019 09:01 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/5 "2019-12-05T09:01:17Z")

</div>

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

```auto
[ {
"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 🙂

---

<div class="post-metadata">

**Author:** ![rajendran20](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rajendran20/32/14706_2.png) [@rajendran20](https://discourse.nodered.org/u/rajendran20)\
**Post date:** [5 December 2019 09:09 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/6 "2019-12-05T09:09:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rajendran20](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rajendran20/32/14706_2.png) [@rajendran20](https://discourse.nodered.org/u/rajendran20)\
**Post date:** [5 December 2019 09:14 UTC](https://discourse.nodered.org/t/convert-csv-format-to-json-format/18810/7 "2019-12-05T09:14:21Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/03a8cb0e0c8c66804bf6b6d190983b5c379e0cd7.png)

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