# Change JSON output (to line protocol or other JSON)

**URL:** <https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425>\
**Category:** General\
**Created:** [18 April 2022 09:00 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425 "2022-04-18T09:00:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![m8dhouse](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m8dhouse/32/59869_2.png) [@m8dhouse](https://discourse.nodered.org/u/m8dhouse)\
**Post date:** [18 April 2022 09:00 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425/1 "2022-04-18T09:00:45Z")

</div>

Hi I have a solar data input that gives me a JSON. I need to convert it to line protocol.

Input:  
{"forecasts":[{"pv\_estimate":2.986,"pv\_estimate10":2.923,"pv\_estimate90":2.986,"period\_end":"2022-04-17T09:00:00.0000000Z","period":"PT30M"},{"pv\_estimate":3.2842,"pv\_estimate10":3.1522,"pv\_estimate90":3.2842,"period\_end":"2022-04-17T09:30:00.0000000Z","period":"PT30M"}, ...]}

Output:  
sunhours,source=solcast pv\_estimate=2.986,pv\_estimate10=2.923,pv\_estimate90=2.986 1650186000000  
sunhours,source=solcast pv\_estimate=3.2842,pv\_estimate10=3.1522,pv\_estimate90=3.2842 1650186000000  
...

Struggling to get started to load the json into an array. I usually use other tools for this so relatively now to the node-red syntax/nodes.

Also tried to look at JSONATA - if easier changing to a different json format is an option:

{  
"measurement": "sunhours",  
"tags": {  
"source": "solcast"  
},  
"fields": {  
"pv\_estimate": 2.986,  
"pv\_estimate10": 2.923,  
"pv\_estimate90": "2.986 "  
},  
"timestamp": 1650186000000  
}  
...

---

<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:** [18 April 2022 14:09 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425/2 "2022-04-18T14:09:09Z")

</div>

try

```auto
[{"id":"edf6c1fde29fbbc9","type":"inject","z":"2b928680670e4e84","name":"Off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"forecasts\":[{\"pv_estimate\":2.986,\"pv_estimate10\":2.923,\"pv_estimate90\":2.986,\"period_end\":\"2022-04-17T09:00:00.0000000Z\",\"period\":\"PT30M\"},{\"pv_estimate\":3.2842,\"pv_estimate10\":3.1522,\"pv_estimate90\":3.2842,\"period_end\":\"2022-04-17T09:30:00.0000000Z\",\"period\":\"PT30M\"}]}","payloadType":"json","x":270,"y":160,"wires":[["28f7e0e8.d86e98"]]},{"id":"28f7e0e8.d86e98","type":"change","z":"2b928680670e4e84","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$join(\t $$.payload.forecasts.(\t \"sunhours,source=solcast\\npv_estimate=\" & $.pv_estimate & \t \",pv_estimate10=\" & $.pv_estimate10 & \t \",pv_estimate90=\" & $.pv_estimate90 & \" \" &\t $toMillis($.period_end)\t ),\t \"\\n\"\t)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":160,"wires":[["d042f113ad2a7a3a"]]},{"id":"d042f113ad2a7a3a","type":"debug","z":"2b928680670e4e84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":220,"wires":[]}]

```

```auto
$join(
   $$.payload.forecasts.(
        "sunhours,source=solcast\npv_estimate=" & $.pv_estimate & 
        ",pv_estimate10=" & $.pv_estimate10 & 
        ",pv_estimate90=" & $.pv_estimate90 & " " &
        $toMillis($.period_end)
   ),
   "\n"
)

```

[edit]  
Or if you want an array of objects

```auto
$$.payload.forecasts.{
    "measurement": "sunhours",
    "tags": {
        "source": "solcast"
    },
    "fields": {
        "pv_estimate": $.pv_estimate,
        "pv_estimate10": $.pv_estimate10,
        "pv_estimate90": $.pv_estimate90
    },
    "timestamp": $toMillis($.period_end)
}

```

---

<div class="post-metadata">

**Author:** ![rakgupta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rakgupta/32/42535_2.png) [@rakgupta](https://discourse.nodered.org/u/rakgupta)\
**Post date:** [18 April 2022 14:30 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425/3 "2022-04-18T14:30:12Z")

</div>

If you want to get the two readings separately, you can modify @E1cid's solution and leave out the join

```auto
[{"id":"d902ee2.6795d1","type":"inject","z":"33292b7b.b59f84","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"forecasts\":[{\"pv_estimate\":2.986,\"pv_estimate10\":2.923,\"pv_estimate90\":2.986,\"period_end\":\"2022-04-17T09:00:00.0000000Z\",\"period\":\"PT30M\"},{\"pv_estimate\":3.2842,\"pv_estimate10\":3.1522,\"pv_estimate90\":3.2842,\"period_end\":\"2022-04-17T09:30:00.0000000Z\",\"period\":\"PT30M\"}]}","payloadType":"json","x":295.3333435058594,"y":979.0000381469727,"wires":[["26be9975.f46036"]]},{"id":"26be9975.f46036","type":"change","z":"33292b7b.b59f84","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.forecasts.(\t \"sunhours,source=solcast\\npv_estimate=\" & $.pv_estimate & \",pv_estimate10=\" &\t $.pv_estimate10 & \",pv_estimate90=\" & $.pv_estimate90 & \" \" &\t $toMillis($.period_end) \t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":476.3333740234375,"y":979,"wires":[["1793a4c3.ce3b23"]]},{"id":"1793a4c3.ce3b23","type":"debug","z":"33292b7b.b59f84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":665.3333435058594,"y":978.0000610351562,"wires":[]}]

```

The output will look like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9325beee53986df2c4314ce460ff32f35ad86b6.jpeg)

---

<div class="post-metadata">

**Author:** ![m8dhouse](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m8dhouse/32/59869_2.png) [@m8dhouse](https://discourse.nodered.org/u/m8dhouse)\
**Post date:** [18 April 2022 16:44 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425/4 "2022-04-18T16:44:56Z")

</div>

> [@E1cid](#):
>
> ```auto
> [{"id":"edf6c1fde29fbbc9","type":"inject","z":"2b928680670e4e84","name":"Off","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"forecasts\":[{\"pv_estimate\":2.986,\"pv_estimate10\":2.923,\"pv_estimate90\":2.986,\"period_end\":\"2022-04-17T09:00:00.0000000Z\",\"period\":\"PT30M\"},{\"pv_estimate\":3.2842,\"pv_estimate10\":3.1522,\"pv_estimate90\":3.2842,\"period_end\":\"2022-04-17T09:30:00.0000000Z\",\"period\":\"PT30M\"}]}","payloadType":"json","x":270,"y":160,"wires":[["28f7e0e8.d86e98"]]},{"id":"28f7e0e8.d86e98","type":"change","z":"2b928680670e4e84","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$join(\t $.payload.forecasts.(\t \"sunhours,source=solcast\\npv_estimate=\" & $.pv_estimate & \t \",pv_estimate10=\" & $.pv_estimate10 & \t \",pv_estimate90=\" & $.pv_estimate90 & \" \" &\t $toMillis($.period_end)\t ),\t \"\\n\"\t)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":160,"wires":[["d042f113ad2a7a3a"]]},{"id":"d042f113ad2a7a3a","type":"debug","z":"2b928680670e4e84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":220,"wires":[]}]
> 
> ```
> 
> ```auto
> 
> ```

Thanks - this works. Good example for me to dissect and learn from!

---

<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:** [2 May 2022 16:45 UTC](https://discourse.nodered.org/t/change-json-output-to-line-protocol-or-other-json/61425/5 "2022-05-02T16:45:36Z")

</div>

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