Skip to content

storing multi-rows data in PostgreSQL DB from a single message #3146

@ngjaying

Description

@ngjaying

Discussed in #3141

Originally posted by robodevchampion August 23, 2024
I have data coming like below after executing SQL

{'device_name': 'sensor2', 'ts': '1724231947811', 'values': {'combine': '41:107', 'humidity': 41, 'temperature': '30'}}

Added an action for insertion data in DB, I have enabled send single as False

Below is the data template I am using

[
  {{- $firstMsg := true -}}
  {{- range $i, $msg := . -}}
    {{- $firstKey := true -}}
    {{- range $key, $value := $msg.values -}}
      {{- if not $firstKey -}},{{- end -}}
      {{- $firstKey = false -}}
      {
        "deviceid": "{{$msg.device_name}}",
        "ts": "{{$msg.ts}}",
        "key_name": "{{$key}}",
        "value": "{{$value}}"
      }
    {{- end -}}
    {{- if not $firstMsg -}},{{- end -}}
    {{- $firstMsg = false -}}
  {{- end -}}
]

Below is the error I got:

fail to decode data [{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "combine",
        "value": "40:107"
      },{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "humidity",
        "value": "40"
      },{
        "deviceid": "sensor1",
        "ts": "1724231947811",
        "key_name": "temperature",
        "value": "200"
      }]
 after applying dataTemplate for error json: cannot unmarshal array into Go value of type map[string]interface {}

Can you help me what I am missing, above data format is desired.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions