# Convert between different types of JSON

**URL:** <https://discourse.nodered.org/t/convert-between-different-types-of-json/25808>\
**Category:** General\
**Created:** [30 April 2020 10:32 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808 "2020-04-30T10:32:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [30 April 2020 10:32 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/1 "2020-04-30T10:32:08Z")

</div>

Just wondering if anyone knows an easy way to convert between different types of JSON? I've had a look at an online converter but it wanted to wrap each row in curly quotes whereas I'm after:-

```auto
{
    "111222333": "the ball and chain",
    "123456789": "ted",
    "213546879": "harry",
    "312645978": "billy",
    "987654321": "sarah"
}

```

this format instead of

```auto
[
 {
   "number": "111222333",
   "name": "the ball and chain"
 },
 {
   "number": "123456789, ",
   "name": ""
 },
 {
   "number": "213546879",
   "name": "harry"
 },
 {
   "number": "312645978",
   "name": "billy"
 },
 {
   "number": "987654321",
   "name": "sarah"
 }
]

```

thank you!

---

<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:** [30 April 2020 10:44 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/2 "2020-04-30T10:44:10Z")

</div>

Hi @dclear

I'm not clear from your two example pieces of JSON which you want to convert into the other. Although your post suggests you are starting with something else entirely and you want to get to the first example, but some online tool has given you the second example.

Exactly what format are you starting with?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 April 2020 10:56 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/3 "2020-04-30T10:56:02Z")

</div>

> [@dclear](#):
>
> convert between different types of JSON

Also do you mean JSON (which means the data is a string) or do you mean javascript objects?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 April 2020 11:07 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/5 "2020-04-30T11:07:42Z")

</div>

You are looking for a kind of "transpose" function ?

Try a function node:

```auto
a = msg.payload // array input

o = {}
for(x=0;x<a.length;x++){
    
    o[a[x].number] = a[x].name     
}

return {payload:o}

```

Output:

```auto
{
    "111222333": "the ball and chain",
    "213546879": "harry",
    "312645978": "billy",
    "987654321": "sarah",
    "123456789, ": ""
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 April 2020 12:44 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/6 "2020-04-30T12:44:31Z")

</div>

> [@bakman2](#):
>
> a = msg.payload // array input

I don't think the input is an array.  
I think Object.entries() may be useful here.

> **[Object.entries() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)**
>
> The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 April 2020 12:50 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/7 "2020-04-30T12:50:30Z")

</div>

His input is an array and he wants the ouput as he showed in the first part of his post (it's written in reverse), unless I completely lost it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 April 2020 12:53 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/8 "2020-04-30T12:53:15Z")

</div>

Oh, sorry, you are right, I thought he wanted to convert the first to the second, but it is the other way round.

---

<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:** [30 April 2020 12:58 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/9 "2020-04-30T12:58:26Z")

</div>

@bakman2 @Colin this is why I asked him to clarify because I think it is ambiguous as to what formats he has.

---

<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:** [30 April 2020 13:33 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/10 "2020-04-30T13:33:28Z")

</div>

It's a follow on / related to this thread [How to receive a phone number by MQTT then perform an Excel style vlookup to obtain a caller name?](https://discourse.nodered.org/t/how-to-receive-a-phone-number-by-mqtt-then-perform-an-excel-style-vlookup-to-obtain-a-caller-name/25764)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 June 2020 13:33 UTC](https://discourse.nodered.org/t/convert-between-different-types-of-json/25808/11 "2020-06-29T13:33:35Z")

</div>

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