# Formatting JSON Output

**URL:** <https://discourse.nodered.org/t/formatting-json-output/66730>\
**Category:** General\
**Created:** [21 August 2022 14:42 UTC](https://discourse.nodered.org/t/formatting-json-output/66730 "2022-08-21T14:42:57Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 14:42 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/1 "2022-08-21T14:42:57Z")

</div>

I have written a function to regex parse data and I am trying to get it to properly format the output to a global. I am seeing an extra object that I am can't determine where it is coming from.

The current output appears as:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/96a3d61d5def00958f2f11f00256f8376230a68a.png)

I want the type object to be one level higher. I want 'type' to be the 0 object and value would be the only object below 0. So it would read:

0: MICPEAK  
value: 1

My code is:

```auto
var nam = msg.payload.match(/(?<=nam=)((\w+\W\d+)|(\+\d+\W+\w+\W\d+)|(\w+\W+\d+)|(\w+\/\w+\W+\d+))/g);
var source = msg.payload.match(/(?<=src=)((\w+\W\d+)|(\w+\W.\d+))/g);
const meterlist = [];
nam.forEach(obj => {
      meterlist.push({ type: obj.split("#")[0], id: obj.split("#")[1]});
});
 const srclist = [];
source.forEach(obj => {
      srclist.push({ type: obj.split("#")[0], id: obj.split("#")[1] });
});
const obj = { meters: meterlist, srclist: srclist};
global.set("meterenum", obj);
msg = obj;
return msg;

```

This code has 2 arrays, If I can get it working on the 1st, then I should be able add the 2nd array to the list.

Here is the flow.

```auto
[
    {
        "id": "0adb481e412468c9",
        "type": "inject",
        "z": "4512ac95.5bc124",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "R1|0|meter 1.src=COD-#1.num=1#1.nam=MICPEAK#1.low=-150.0#1.hi=20.0#1.desc=Signal strength of MIC output in CODEC#1.unit=dBFS#1.fps=40#2.src=COD-#2.num=2#2.nam=MIC#2.low=-150.0#2.hi=20.0#2.desc=Average Signal strength of MIC ouput in CODEC#2.unit=dBFS#2.fps=20#3.src=TX-#3.num=5#3.nam=HWALC#3.low=-150.0#3.hi=20.0#3.desc=Voltage present at the Hardware ALC RCA Plug#3.unit=dBFS#3.fps=20#4.src=RAD#4.num=334#4.nam=+13.8A#4.low=10.5#4.hi=15.0#4.desc=Main radio input voltage at PA#4.unit=Volts#4.fps=0#5.src=RAD#5.num=0#5.nam=+13.8B#5.low=10.5#5.hi=15.0#5.desc=Main radio input voltage at CPU (continuous)#5.unit=Volts#5.fps=0#6.src=RAD#6.num=3#6.nam=MAINFAN#6.low=0.0#6.hi=8192.0#6.desc=Main radio fan RPM#6.unit=RPM#6.fps=0#7.src=TX-#7.num=1#7.nam=FWDPWR#7.low=0.0#7.hi=53.0#7.desc=RF Power Forward#7.unit=dBm#7.fps=20#8.src=TX-#8.num=2#8.nam=REFPWR#8.low=0.0#8.hi=53.0#8.desc=RF Power Reflected#8.unit=dBm#8.fps=20#9.src=TX-#9.num=3#9.nam=SWR#9.low=1.0#9.hi=999.0#9.desc=RF SWR#9.unit=SWR#9.fps=20#10.src=TX-#10.num=4#10.nam=PATEMP#10.low=0.0#10.hi=100.0#10.desc=PA Temperature#10.unit=degC#10.fps=0#74.src=AMP#74.num=0x110F8DDA#74.nam=RL#74.low=0.0#74.hi=20.0#74.desc=External Meter#74.unit=dB#74.fps=0#76.src=AMP#76.num=0x110F8DDA#76.nam=Eff#76.low=0.0#76.hi=100.0#76.desc=External Meter#76.unit=Percent#76.fps=0#143.src=AMP#143.num=0x021B5F36#143.nam=RL#143.low=0.0#143.hi=20.0#143.desc=External Meter#143.unit=dB#143.fps=0#145.src=AMP#145.num=0x021B5F36#145.nam=Eff#145.low=0.0#145.hi=100.0#145.desc=External Meter#145.unit=Percent#145.fps=0#180.src=SLC#180.num=2#180.nam=24kHz#180.low=-150.0#180.hi=10.0#180.desc=24kHz broadband slice receiver signal level#180.unit=dBFS#180.fps=10#181.src=SLC#181.num=2#181.nam=LEVEL#181.low=-150.0#181.hi=10.0#181.desc=Signal strength of signals in the filter passband#181.unit=dBm#181.fps=10#182.src=SLC#182.num=2#182.nam=AGC+#182.low=-150.0#182.hi=0.0#182.desc=Signal strength after AGC#182.unit=dBFS#182.fps=10#183.src=TX-#183.num=10#183.nam=CODEC#183.low=-150.0#183.hi=20.0#183.desc=Signal strength of CODEC output#183.unit=dBFS#183.fps=10#184.src=TX-#184.num=10#184.nam=SC_MIC#184.low=-150.0#184.hi=20.0#184.desc=Signal strength of MIC output#184.unit=dBFS#184.fps=10#185.src=TX-#185.num=10#185.nam=COMPPEAK#185.low=-150.0#185.hi=20.0#185.desc=Signal strength of signals after the CLIPPER (Compression)#185.unit=dBFS#185.fps=20#186.src=TX-#186.num=10#186.nam=SC_FILT_1#186.low=-150.0#186.hi=20.0#186.desc=Signal strength after Filter 1#186.unit=dBFS#186.fps=10#187.src=TX-#187.num=10#187.nam=ALC#187.low=-150.0#187.hi=20.0#187.desc=Signal strength of signals after SW ALC (SSB Peak)#187.unit=dBFS#187.fps=10#188.src=TX-#188.num=10#188.nam=PRE_WAVE_AGC#188.low=-150.0#188.hi=20.0#188.desc=Signal strength before Waveform AGC#188.unit=dBFS#188.fps=10#189.src=TX-#189.num=10#189.nam=PRE_WAVE#189.low=-150.0#189.hi=20.0#189.desc=Signal strength before Waveform Shim#189.unit=dBFS#189.fps=10#190.src=TX-#190.num=10#190.nam=SC_FILT_2#190.low=-150.0#190.hi=20.0#190.desc=Signal strength after Filter 2#190.unit=dBFS#190.fps=10#191.src=TX-#191.num=10#191.nam=B4RAMP#191.low=-150.0#191.hi=20.0#191.desc=Signal strength of signals before the ramp#191.unit=dBFS#191.fps=10#192.src=TX-#192.num=10#192.nam=AFRAMP#192.low=-150.0#192.hi=20.0#192.desc=Signal strength of signals after the ramp#192.unit=dBFS#192.fps=10#193.src=TX-#193.num=10#193.nam=POST_P#193.low=-150.0#193.hi=0.0#193.desc=Signal strength after all processing is done but before attenuation for power control#193.unit=dBFS#193.fps=10#194.src=TX-#194.num=10#194.nam=GAIN#194.low=-150.0#194.hi=0.0#194.desc=Signal strength after Gain#194.unit=dBFS#194.fps=10#195.src=SLC#195.num=3#195.nam=24kHz#195.low=-150.0#195.hi=10.0#195.desc=24kHz broadband slice receiver signal level#195.unit=dBFS#195.fps=10#196.src=SLC#196.num=3#196.nam=LEVEL#196.low=-150.0#196.hi=10.0#196.desc=Signal strength of signals in the filter passband#196.unit=dBm#196.fps=10#197.src=SLC#197.num=3#197.nam=AGC+#197.low=-150.0#197.hi=0.0#197.desc=Signal strength after AGC#197.unit=dBFS#197.fps=10#198.src=TX-#198.num=11#198.nam=CODEC#198.low=-150.0#198.hi=20.0#198.desc=Signal strength of CODEC output#198.unit=dBFS#198.fps=10#199.src=TX-#199.num=11#199.nam=SC_MIC#199.low=-150.0#199.hi=20.0#199.desc=Signal strength of MIC output#199.unit=dBFS#199.fps=10#200.src=TX-#200.num=11 #200.nam=COMPPEAK#200.low=-150.0#200.hi=20.0#200.desc=Signal strength of signals after the CLIPPER (Compression)#200.unit=dBFS#200.fps=20#201.src=TX-#201.num=11#201.nam=SC_FILT_1#201.low=-150.0#201.hi=20.0#201.desc=Signal strength after Filter 1#201.unit=dBFS#201.fps=10#202.src=TX-#202.num=11#202.nam=ALC#202.low=-150.0#202.hi=20.0#202.desc=Signal strength of signals after SW ALC (SSB Peak)#202.unit=dBFS#202.fps=10#203.src=TX-#203.num=11#203.nam=PRE_WAVE_AGC#203.low=-150.0#203.hi=20.0#203.desc=Signal strength before Waveform AGC#203.unit=dBFS#203.fps=10#204.src=TX-#204.num=11#204.nam=PRE_WAVE#204.low=-150.0#204.hi=20.0#204.desc=Signal strength before Waveform Shim#204.unit=dBFS#204.fps=10#205.src=TX-#205.num=11#205.nam=SC_FILT_2#205.low=-150.0#205.hi=20.0#205.desc=Signal strength after Filter 2#205.unit=dBFS#205.fps=10#206.src=TX-#206.num=11#206.nam=B4RAMP#206.low=-150.0#206.hi=20.0#206.desc=Signal strength of signals before the ramp#206.unit=dBFS#206.fps=10#207.src=TX-#207.num=11#207.nam=AFRAMP#207.low=-150.0#207.hi=20.0#207.desc=Signal strength of signals after the ramp#207.unit=dBFS#207.fps=10#208.src=TX-#208.num=11#208.nam=POST_P#208.low=-150.0#208.hi=0.0#208.desc=Signal strength after all processing is done but before attenuation for power control#208.unit=dBFS#208.fps=10#209.src=TX-#209.num=11#209.nam=GAIN#209.low=-150.0#209.hi=0.0#209.desc=Signal strength after Gain#209.unit=dBFS#209.fps=10#245.src=AMP#245.num=0x2756F85F#245.nam=RL#245.low=0.0#245.hi=20.0#245.desc=External Meter#245.unit=dB#245.fps=0#247.src=AMP#247.num=0x2756F85F#247.nam=Eff#247.low=0.0#247.hi=100.0#247.desc=External Meter#247.unit=Percent#247.fps=0#282.src=AMP#282.num=0x333CE553#282.nam=FWD#282.low=0.0#282.hi=63.0#282.desc=External Meter#282.unit=dBm#282.fps=0#283.src=AMP#283.num=0x333CE553#283.nam=RL#283.low=0.0#283.hi=20.0#283.desc=External Meter#283.unit=dB#283.fps=0#284.src=AMP#284.num=0x333CE553#284.nam=ID#284.low=0.0#284.hi=70.0#284.desc=External Meter#284.unit=Amps#284.fps=0#285.src=AMP#285.num=0x333CE553#285.nam=Eff#285.low=0.0#285.hi=100.0#285.desc=External Meter#285.unit=Percent#285.fps=0#286.src=AMP#286.num=0x333CE553#286.nam=TEMP#286.low=0.0#286.hi=100.0#286.desc=External Meter#286.unit=degC#286.fps=0#319.src=SLC#319.num=0#319.nam=24kHz#319.low=-140.0#319.hi=20.0#319.desc=24kHz broadband slice receiver signal level#319.unit=dBFS#319.fps=10#320.src=SLC#320.num=0#320.nam=OSC#320.low=-140.0#320.hi=20.0#320.desc=Signal Strength after OSC#320.unit=dBFS#320.fps=10#321.src=SLC#321.num=0#321.nam=LEVEL#321.low=-150.0#321.hi=20.0#321.desc=Signal strength of signals in the filter passband#321.unit=dBm#321.fps=10#322.src=SLC#322.num=0#322.nam=NR/ANF#322.low=-150.0#322.hi=20.0#322.desc=Signal strength after NR/ANF#322.unit=dBm#322.fps=10#323.src=SLC#323.num=0#323.nam=AGC+#323.low=-150.0#323.hi=0.0#323.desc=Signal strength after AGC#323.unit=dBFS#323.fps=10#324.src=TX-#324.num=8#324.nam=CODEC#324.low=-150.0#324.hi=20.0#324.desc=Signal strength of CODEC output#324.unit=dBFS#324.fps=10#325.src=TX-#325.num=8#325.nam=SC_MIC#325.low=-150.0#325.hi=20.0#325.desc=Signal strength of MIC output#325.unit=dBFS#325.fps=10#326.src=TX-#326.num=8#326.nam=COMPPEAK#326.low=-150.0#326.hi=20.0#326.desc=Signal strength of signals after the CLIPPER (Compression)#326.unit=dBFS#326.fps=20#327.src=TX-#327.num=8#327.nam=SC_FILT_1#327.low=-150.0#327.hi=20.0#327.desc=Signal strength after Filter 1#327.unit=dBFS#327.fps=10#328.src=TX-#328.num=8#328.nam=ALC#328.low=-150.0#328.hi=20.0#328.desc=Signal strength of signals after SW ALC (SSB Peak)#328.unit=dBFS#328.fps=10#329.src=TX-#329.num=8#329.nam=PRE_WAVE_AGC#329.low=-150.0#329.hi=20.0#329.desc=Signal strength before Waveform AGC#329.unit=dBFS#329.fps=10#330.src=TX-#330.num=8#330.nam=PRE_WAVE#330.low=-150.0#330.hi=20.0#330.desc=Signal strength before Waveform Shim#330.unit=dBFS#330.fps=10#331.src=TX-#331.num=8#331.nam=SC_FILT_2#331.low=-150.0#331.hi=20.0#331.desc=Signal strength after Filter 2#331.unit=dBFS#331.fps=10#332.src=TX-#332.num=8#332.nam=B4RAMP#332.low=-150.0#332.hi=20.0#332.desc=Signal strength of signals before the ramp#332.unit=dBFS#332.fps=10#333.src=TX-#333.num=8#333.nam=AFRAMP#333.low=-150.0#333.hi=20.0#333.desc=Signal strength of signals after the ramp#333.unit=dBFS#333.fps=10#334.src=TX-#334.num=8#334.nam=POST_P#334.low=-150.0#334.hi=0.0#334.desc=Signal strength after all processing is done but before attenuation for power control#334.unit=dBFS#334.fps=10#335.src=TX-#335.num=8#335.nam=GAIN#335.low=-150.0#335.hi=0.0#335.desc=Signal strength after Gain#335.unit=dBFS#335.fps=10#336.src=SLC#336.num=1#336.nam=24kHz#336.low=-140.0#336.hi=20.0#336.desc=24kHz broadband slice receiver signal level#336.unit=dBFS#336.fps=10#337.src=SLC#337.num=1#337.nam=OSC#337.low=-140.0#337.hi=20.0#337.desc=Signal Strength after OSC#337.unit=dBFS#337.fps=10#338.src=SLC#338.num=1#338.nam=LEVEL#338.low=-150.0#338.hi=20.0#338.desc=Signal strength of signals in the filter passband#338.unit=dBm#338.fps=10#339.src=SLC#339.num=1#339.nam=NR/ANF#339.low=-150.0#339.hi=20.0#339.desc=Signal strength after NR/ANF#339.unit=dBm#339.fps=10#340.src=SLC#340.num=1#340.nam=AGC+#340.low=-150.0#340.hi=0.0#340.desc=Signal strength after AGC#340.unit=dBFS#340.fps=10#341.src=TX-#341.num=9#341.nam=CODEC#341.low=-150.0#341.hi=20.0#341.desc=Signal strength of CODEC output#341.unit=dBFS#341.fps=10#342.src=TX-#342.num=9#342.nam=SC_MIC#342.low=-150.0#342.hi=20.0#342.desc=Signal strength of MIC output#342.unit=dBFS#342.fps=10#343.src=TX-#343.num=9#343.nam=COMPPEAK#343.low=-150.0#343.hi=20.0#343.desc=Signal strength of signals after the CLIPPER (Compression)#343.unit=dBFS#343.fps=20#344.src=TX-#344.num=9#344.nam=SC_FILT_1#344.low=-150.0#344.hi=20.0#344.desc=Signal strength after Filter 1#344.unit=dBFS#344.fps=10#345.src=TX-#345.num=9#345.nam=ALC#345.low=-150.0#345.hi=20.0#345.desc=Signal strength of signals after SW ALC (SSB Peak)#345.unit=dBFS#345.fps=10#346.src=TX-#346.num=9#346.nam=PRE_WAVE_AGC#346.low=-150.0#346.hi=20.0#346.desc=Signal strength before Waveform AGC#346.unit=dBFS#346.fps=10#347.src=TX-#347.num=9#347.nam=PRE_WAVE#347.low=-150.0#347.hi=20.0#347.desc=Signal strength before Waveform Shim#347.unit=dBFS#347.fps=10#348.src=TX-#348.num=9#348.nam=SC_FILT_2#348.low=-150.0#348.hi=20.0#348.desc=Signal strength after Filter 2#348.unit=dBFS#348.fps=10#349.src=TX-#349.num=9#349.nam=B4RAMP#349.low=-150.0#349.hi=20.0#349.desc=Signal strength of signals before the ramp#349.unit=dBFS#349.fps=10#350.src=TX-#350.num=9#350.nam=AFRAMP#350.low=-150.0#350.hi=20.0#350.desc=Signal strength of signals after the ramp#350.unit=dBFS#350.fps=10#351.src=TX-#351.num=9#351.nam=POST_P#351.low=-150.0#351.hi=0.0#351.desc=Signal strength after all processing is done but before attenuation for power control#351.unit=dBFS#351.fps=10#352.src=TX-#352.num=9#352.nam=GAIN#352.low=-150.0#352.hi=0.0#352.desc=Signal strength after Gain#352.unit=dBFS#352.fps=10#",
        "payloadType": "str",
        "x": 850,
        "y": 720,
        "wires": [
            [
                "a56d0fae32b932c4",
                "0497cb65875a0c04"
            ]
        ]
    },
    {
        "id": "a56d0fae32b932c4",
        "type": "function",
        "z": "4512ac95.5bc124",
        "name": "function 1",
        "func": "var nam = msg.payload.match(/(?<=nam=)((\\w+\\W\\d+)|(\\+\\d+\\W+\\w+\\W\\d+)|(\\w+\\W+\\d+)|(\\w+\\/\\w+\\W+\\d+))/g);\nvar source = msg.payload.match(/(?<=src=)((\\w+\\W\\d+)|(\\w+\\W.\\d+))/g);\nconst meterlist = [];\nnam.forEach(obj => {\n meterlist.push({ type: obj.split(\"#\")[0], id: obj.split(\"#\")[1]});\n});\n const srclist = [];\nsource.forEach(obj => {\n srclist.push({ type: obj.split(\"#\")[0], id: obj.split(\"#\")[1] });\n});\nconst obj = { meters: meterlist, srclist: srclist};\nglobal.set(\"meterenum\", obj);\nmsg = obj;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1000,
        "y": 640,
        "wires": [
            [
                "d99a93d835d6438a"
            ]
        ]
    },
    {
        "id": "d99a93d835d6438a",
        "type": "debug",
        "z": "4512ac95.5bc124",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1140,
        "y": 720,
        "wires": []
    }
]

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 August 2022 16:28 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/2 "2022-08-21T16:28:09Z")

</div>

```auto
meterlist.push({[obj.split("#")[0]]: {value: obj.split("#")[1]}});

```

should work, but your info/code is confusing as your code referenced id not type.

But i would probably not use regex, the below should work, but not 100% sure what info out of the original string you are actually trying to parse.

```auto
const srclist =[];
let meters = msg.payload.split("src=").slice(1,-1);
meters = meters.map(str =>{
    str = str.split(".").map(str1 => str1.split("="));
    let srcbits = str[0][0].split("#");
    srclist.push({type: srcbits[0], id: srcbits[1]})
    return {[str[2][1].split("#")[0]]: {value:str[1][1].split("#")[1]}}
})
msg.payload= {meters, srclist}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 16:45 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/3 "2022-08-21T16:45:46Z")

</div>

The flow has an inject node with the string data. I will give this a shot.

thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 August 2022 16:56 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/4 "2022-08-21T16:56:03Z")

</div>

Yes i saw the inject input, but am not sure of the data you want parsed from it.  
i.e.  
which bits in this string refers to meterlist type and value, and which is the srclist type and id

```auto
src=COD-#1.num=1#1.nam=MICPEAK#1.low=-150.0#1.hi=20.0#1.desc=Signal strength of MIC output in CODEC#1.unit=dBFS#1.fps=40#2

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 16:59 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/5 "2022-08-21T16:59:58Z")

</div>

you got me 99% there. I want to create a json output with :  
nam='value'  
id = #1 after nam  
src='value'

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 August 2022 17:03 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/6 "2022-08-21T17:03:15Z")

</div>

Still confusing, you need to be 100% clear, otherwise we are guessing and will only get 99% correct.

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 19:58 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/7 "2022-08-21T19:58:31Z")

</div>

Here is one record of the 85 that are in the inject string. It is the 1st record. Each record is associated with a unique int. In this case it is 1.

`1. **src=COD** -#1.num=1#1. **nam=MICPEAK** #1.low=-150.0#1.hi=20.0#1.desc=Signal strength of MIC output in CODEC#1.unit=dBFS#1.fps=40`

MICPEAK is the type of meter  
1 is the value (indices)  
COD is the source.

Originally I had use 2 different regex parses to get the data. I would like the output to be:

Object: MICPEAK  
Source = COD  
Value = 1

Hope this is clearer.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 August 2022 20:49 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/8 "2022-08-21T20:49:20Z")

</div>

Still murky.  
which 1?  
There are numerous.

```auto
let meters = msg.payload.split("src=").slice(1,-1);
meters = meters.map(str => {
    str = ("src=" + str).split(".").map(str1 => str1.split("="));
    let bits = str[0][1].split(/-?#/);
    return {
        [str[2][1].split("#")[0]]: {
            value: bits[1],
            source: bits[0]
        }
    }
})
msg = {meters}
return msg;

```

[edit]  
added code for value at this position `nam=MICPEAK**#1`

```auto
let meters = msg.payload.split("src=").slice(1,-1); // split string on "src", remove first item in array.
meters = meters.map(str =>{ // loop through array
    str = ("src=" + str).split(".").map(str1 => str1.split("=")); // split on "." the convert each array item by splitting again on "=".
    let bits = str[2][1].split("#"); // split array 2 item 1 on "#"
    return { //create object 1
        [bits[0]]: { // assign property and make it an object 2
            value: bits[1], // add value property
            source: str[0][1].split(/-?#/)[0] //add source property
        } // close object 2
    } //close object 1
}) //close map
msg= {meters} // assign meters to msg giving same property name as variable meters
return msg;

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 20:56 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/9 "2022-08-21T20:56:51Z")

</div>

This one.

```auto
nam=MICPEAK**#1

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [21 August 2022 21:14 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/10 "2022-08-21T21:14:07Z")

</div>

I am looking at your code and I don't see why we can't use SRC like you have done. I have checked against a couple of string sources and I don't see any errors or offsets. Now trying to understand your methodology.

thanks

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [22 August 2022 16:20 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/11 "2022-08-22T16:20:58Z")

</div>

Your code works fine. I still have one question. I wanted to use 'nam' as the reference obj after I push the function to a global variable.

```auto
let meters = msg.payload.split("src=").slice(1, -1); // split string on "src", remove first item in array.
meters = meters.map(str => { // loop through array
    str = ("src=" + str).split(".").map(str1 => str1.split("=")); // split on "." the convert each array item by splitting again on "=".
    let bits = str[2][1].split("#"); // split array 2 item 1 on "#"
     return { //create object 1
        [bits[0]]: { // assign property and make it an object 2
            meterid: bits[1], // add value property
            source: str[0][1].split(/-?#/)[0], //add source property
            // map: str.map(str1 => str1),
            desc: str[7][1],
            unit: str[8][1].split(/-?#/)[0],
            fps: str[9][1].split(/-?#/)[0]
        }// close object 2
    } //close object 1
}) //close map
global.set('metersEnum', meters);
msg = { meters } // assign meters to msg giving same property name as variable meters
return msg;

```

when I do a global.get(metersEnum) I see:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e3c82c1b9136c515fe0531d84d28e0b1bf104a8.png)

In order for me to reference MICPEAK in this case I still have to use the array indices

```auto
msg = global.get('metersEnum[0]');
return msg;

```

Since the indices location can change using the array location defeats the purpose. I wanted to be able to use

```auto
msg = global.get('metersEnum.MICPEAK');
return msg;

```

Thanks for your thoughts on this.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 August 2022 16:57 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/12 "2022-08-22T16:57:36Z")

</div>

If you want an object then use array.reduce.

```auto
let meters = msg.payload.split("src=").slice(1,-1); 
meters = meters.reduce((acc,str) =>{ 
    str = ("src=" + str).split(".").map(str1 => str1.split("=")); 
    let bits = str[2][1].split("#"); 
    acc[bits[0]] = { 
        value: bits[1], 
        source: str[0][1].split(/-?#/)[0] 
    } 
    return acc; 
},{}) 
msg= {meters} 
return msg;

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [22 August 2022 17:37 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/13 "2022-08-22T17:37:20Z")

</div>

Thank you. perfect.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b9ab17ca44797b14b8dbfc51041e7386abea94d.png)

---

<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:** [5 September 2022 17:38 UTC](https://discourse.nodered.org/t/formatting-json-output/66730/14 "2022-09-05T17:38:15Z")

</div>

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