I am trying to get a dynamic json (number of rows can vary) in a markdown card, formatted in a HTML table (for later formatting)
after many iterations (and trail and error (mainly) I ended up with this piece of yaml for the card
type: markdown
title: Crypto Prices
content: >
<table>
<tr>
<th>Token</th>
<th>Value</th>
</tr>
{% for item in states.sensor.crypto_prices.state %}
<tr>
<td>{{ item.keys() | list | first }}</td>
<td>{{ item.values() | list | first }}</td>
</tr>
{% endfor %}
</table>
the json looks like this ( values adjusted for this post)
So your JSON seems to be valid and your code seems appropriate. You’ll notice I substituted states.sensor.crypto_prices.state for value_json to make it work in the Template Editor, since I don’t have that sensor to work with. You may want to try using the more widely accepted format of states('sensor.crypto_prices') and see if that works. If not, I’d look very closely at the data written to that sensor. There is either a missing set of quotes, extra quotes or the more difficult to spot improper quotes. In essence, if your sensor encapsulates the JSON string with double quotes, there can be NO double quotes within that sting. They MUST be replaced with single quotes.
For example "{{ state_attr('sensor.current_forecast', 'current_forecast') }}" is OK, but '{{ state_attr('sensor.current_forecast', 'current_forecast') }}' will fail, as would "{{ state_attr("sensor.current_forecast", "current_forecast") }}"
*** Note; this is just a quick example to illustrate my point, and not the data you would find in the JSON data, but the principal remains the same. You must encapsulate quoted data with the opposite type of quotes used within the data. Good luck!!
Edit In looking at some of my sensors with JSON data as a string, there are NO quotes, so it is possible that a rogue quote in your text could also break the code. Of course your dataset may be much bigger than the sample you provided so it’s impossible to check that at this point. try pasting your JSON data into this website: https://jsonlint.com and also this one to get the correct paths: https://jsonpathfinder.com
Many thanks @kartcon for taking the time and effort looking into this
I have been working with your hints and tips through out the day and have come … almost nowhere sadly.
I am really confused as I have been working with json/xml and other data formats for a really long time now, and neve had a huddle this big to jump over.
New to HA and how data is processed, this is clearly due to me missing something crucial, somewhere.
the JSON is valid and clean, no hidden special characters or " or ’ anywhere.
tried different method iterating through the dataset, no luck.
Learned from the past, making things simple before complicating, I went back to the below code in the card.
That is giving me am expected single cell with data <template TemplateState(<state sensor.crypto_prices_v2=[{'XRP': 370.4321873322}, {'SLP': 13.458802627621148}, {'SOL': 489.61819747019996}, {'BTC': 316.06514364}, {'DOGE': 100.54670776689922}]; friendly_name=Crypto Prices V2 @ 2025-01-25T17:53:27.064513+01:00>)>
Hinting me to… nowhere.
google is not a great help on this, so trying the best of luck here once more.
else I will have to find a different way in the backend providing data differently, Would be sad if I have to move to this direction.
Thanks again for any hints and tips or examples, pushing me in the right way!
Can you post the entire value of sensor.crypto_prices_v2 or whatever sensor the rest command posts its data to. I’d like to create a local sensor with the full dataset so I can test. I do think you’re very close but missing something. I’m really thinking it has something to do with how the JSON is written to the sensor, so having the full dataset will allow me to better troubleshoot. I understand if you need to adjust the values for security or privacy reasons, just please do not remove anything that may nullify the dataset. Changing values should not affect the data, so that’s OK by me.
After a few trial and error @kartcon has found the solution to the problem.
The issue is to be found in the way HA is attacking the JSON it seems and parsing the JSON string as text, converting it into an array using split, removing unwanted characters while parsing through the lines, making them visible in the table.
A rather complex way to iterate through a valid JSON string, but it works!