Need help with json/dictionary to csv and back

Hi!
I want to save a JSON, (stored in dictionary) to a CSV file, and i want to save it that way i can import it back later. I've seen @Juan_Antonio tutorial about this, but i cant figure it out how to do it in my case.I have a json or dictionary with list of lists or 2dim array or call it table or whatever you want. So looks like this:

{"PID":1,"RGBW":[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],"Dims":[100,100,100,100,100,100,100,100]}

How can i save it to a CSV that i can import it from.

This is tricky.

You actually have two separate tables in your JSON, with different shapes.
How about saving them into two different csv files, RGBW.csv and Dims.csv ?

Alternatively,
Does the number of rows in Rgbw (=8) and Dims (=8) always match?
If so, you could export a flat csv with these headers:

  • PID
  • R
  • G
  • B
  • W
  • Dim

In this case, you would get a 6 column table with 9 (= 1 + 8) rows.
You would need a for each number loop to traverse the RGBW and DIM lists together in lock step while outputting your CSV.

This is tricky.

Yeah, i thought it.
And i thought about 2 csv files, but i want it too be like a saved profile, to be easy to import, and one file would be more simple. About the RGBW and Dims table, yes, they have the same item numbers. The Dims is the dim value of an RGBW table. I'm not happy about separating the RGBW lists to four value, but if this is the way, then so be it.
Thank you for the idea. I'm starting to make something from it, maybe in the meantime someone will come with other ideas.

Me, I would get it into a list and keep it as a list and work with it as a list - one way...

Ok. This is a great idea, but i already have the 2 lists available separated. Though maybe not a huge work to make them one. I will look into it. Thank you!

I tried with this type of list, but it made a weird csv. To tired today. I will continue tomorrow.

Something like this:

Do It Result: [1, [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100], [[0, 0, 0, 0], 100]]

image

Thank you Tim!
I have managed to do this part of the job myself


the csv result is this:
Capture
Bit i'm struggling with load it back to lists. i guess the problem is the RGB list is not really a list in the csv. No comma, no quotes, instead there is brackets.

By the way. Why is do it greyed out for me?

Ok, i think the csv table cannot handle list as an element in the row. Then there is 2 possible solutions. I add the Dim value to the RGBW list as the fifth element and only after this i add the RGBW to the csv table, or i add the dim table separately after or before the RGBW table so i will have a table with rownumbers of (RGBW table row plus Dim table row, plusz PID row plus other if i add more)

I have an other question.
Replace list item block cannot replace item if a list item is also a list?
I can add a list to an other list as element but cannot replace it? Grrr. No logic in it.

I find this tricky too, since the lists are pointers to storage locations and you can end up with a tangle. I use the copy list block into a separate variable for the row I want to update, update the temporary copy, then replace the row in the original table with the updated row copy.

Alternatively, if the data being updated is worth saving, consider building up a compound text key for TinyDB or your database including the row and column number, delimitted for convenience and uniqueness i.e. 'row2col5'

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

Sure.
I saved a 2dim array (list of lists) (plusz some other variable) to a csv file by the following:

lists_to_csv_table
CSV table after this:

"100"
"0","0","0","0"
"0","0","0","0"
"0","0","0","0"
"0","9","255","0"
"0","0","0","0"
...
"100","100","100","100","100","100","100",......

This is ok.
After this i wanted to load it back to lists.


This is how it works now, but since every row is a list, i wanted to load it them back by one replace per row. Like this:

Since i can add a list as an element to another list by add items to list and i can set a whole list like i did with the DimMatrix above, I thought i also can replace a list element as a list.
As @ABG mentioned it should be a pointer to a memory loction, so replace also should work, BUT seems there is another logic behind it. This is the story.
It works replace element by element, but if you can enlighten us why it does not work with list, we would be glad.

Do you have lists to json unticked in your designer ?

What you see is a list - "(0 0 0 0)" but you will see my one comes out like this "[0 0 0 0]"
you just need to dig down into the list of lists structure to get at each element.

You mean this one?:
Capture
Yes it is unthicked.
But are you saying If i check this box and i get the same result like you "[0 0 0 0]", i still have to retrieve each element one by one like i do it now? Then it doesn't matter if it is in list format or json format, isn't it?
Please, correct me if i misunderstand you.
By the way, i separated the Dim table from the RGBW table

, but i still have to retrieve each element one by one. I cannot load a row to a list with replace . So what does the checkbox help me in this?

  1. Just concerned that with the parantheses ( ) instead of the square brackets [ ] you may have thought it wasn't a list....
  2. "There are many ways to skin a cat", it is up to you how you organise your list of lists, just seemed to make sense to me to put each set of RGBW values and the respective DIM value together in a sublist.

Let's talk about the variable R.G.B.W.

Their names hint at they're being the 4 components of a color (Red, Green, Blue, (?)Whiteness/Opacity).

If so, you can drop a dimension from your problem and combine them into a single 4 byte number using the make_color block from the Color blocks palette. There is also a complementary block there for reversing this, taking a color value and splitting it into 4 parts.

That would flatten your 3 dimensional structure into a 2 dimensional structure, fitting nicely into csv tables.

I see. You are right, it is make sense to put them together, but separatedly is easier to load them back. Thank you for your help, and explanation.

@ABG The W is white. It is RGBW LED. And i didn't converted it to color because i use these list to other purpose (sending to ESP32 by web block) too where i need the list, not the 4 byte color code and i didnt want to make other lists for save. Besides RGBW and Dim there are other variable i added that is intervene with the pure 2dim table.
Thank you for the idea, i tried it that way too. Made a table with 5 elements in a row. That way i have to make the 5 element lists and after load i have to separate it, this way i have to load the values one by one. i don't know what is better, but i think the last one is maybe better.
Anyway, it is works, thank you all your helps.

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