flex-table-card icon indicating copy to clipboard operation
flex-table-card copied to clipboard

Using flex table card with JSON

Open galtamur opened this issue 2 years ago • 7 comments

Hello, I am trying to use the Flex Table Card with a sensor providing a JSON coming from a query. The JSON is as follows: {"my_data":[{"field_a":"a","field_b":"b"},{"field_a":"a","field_b":"b"}]} If I configure the card this way: columns:

  • name: hhh data: prova modify: x I see the string of the json as result.

If I configure the card this way: columns:

  • name: hhh data: prova modify: x.field_a I see undefinedundefinedundefined as result.

Where is my error? Or there is an issue somewhere? Thank you for helping.

galtamur avatar May 10 '22 09:05 galtamur

mmmh, if the json data is as you say then I would maybe try:

modify: x.my_data[0].field_a

maybe with quotes:

modify: 'x.my_data[0].field_a'

because you have a map (field_a) inside a list ([0]) inside a map (my_data)

daringer avatar May 10 '22 15:05 daringer

It's showing nothing... Neither with nor without quotes, neither with or wthout [0]. By the way, it's showing nothing, not undefinedundefinedundefined. I also tried x[0] and it shows the first character of the string, i.e. "{". Looks like he sees the string as... a string, and not as a json object.

galtamur avatar May 10 '22 15:05 galtamur

oh, then you could try:

modify: JSON.parse(x).my_data[0].field_a

daringer avatar May 11 '22 13:05 daringer

I tried this, and it's better, but it shows the first record. In other words, out of: {"my_data":[{"field_a":"a1","field_b":"b"},{"field_a":"a2","field_b":"b"}]} it shows a1, whereas I expect to see a table with a column with to rows, with a1 and a2 respectively... Ideas?

galtamur avatar May 11 '22 14:05 galtamur

oh, nope this will not work to expand the json parsed with modify into multiple rows, sorry

daringer avatar May 12 '22 11:05 daringer

Hi Markus

I too am really struggling with this - it would really be nice to just be able to feed json data into this (totally awesome) card

is this something that could be supported (or already is and I am misunderstanding how to implement it)?

    - type: 'custom:flex-table-card'
      title: Load from Json
      strict: false
      columns:
      - data: AA
      - data: BB
      - data: CC

      entities:
        include: [{"AA":9.3,"BB":380,"CC":1300},{"AA":1.3,"BB":180,"CC":1100}]

Or this

    - type: 'custom:flex-table-card'
      title: Load from Json
     ...
      entities: [{"AA":9.3,"BB":380,"CC":1300},{"AA":1.3,"BB":180,"CC":1100}]

Or this

    - type: 'custom:flex-table-card'
      title: Load from Json
     ...
      json: [{"AA":9.3,"BB":380,"CC":1300},{"AA":1.3,"BB":180,"CC":1100}]

teskanoo avatar Oct 10 '22 02:10 teskanoo

To display the JSON in the original comment, from an entity input_text.mydata with an attribute prova, you could use:

type: custom:flex-table-card
title: My Data
entities:
  include: input_text.mydata
columns:
  - name: Field A
    data: prova.my_data
    modify: x.field_a
  - name: Field B
    data: prova.my_data
    modify: x.field_b

MyData

EdLeckert avatar Sep 19 '23 23:09 EdLeckert