Page 1 of 1

Maps and JSON tofile fromfile

Posted: 22 May 2019 07:02
by yogi108
Hi,

I am at a point to store some data and like to use JSON format,
combined with maps into a file.

The flow creates the map:
cMap=newMapFromValues("triggertime",triggertime,"name","Martin","phone","+486145558","info","Info");
js=toJSON(cMap);

The action stores the variable js into the file, all OK.
Next time the flow starts I read the stored map, do a
cMap=from JSON(var);

Now I want to have the next data
cMap1=newMapFromValues("triggertime",triggertime,"name","Desmanto","phone","+626163451","info","Info1");

combined with cMap, stored as JSON again in the file, so now there are 2 records.
How to add cMap and cMap1?

Thanks a lot,
Regards,

Re: Maps and JSON tofile fromfile

Posted: 22 May 2019 11:11
by anuraag
There are 2 ways

1st method
Create a list and store cMap in it.

Code: Select all

cList=newList();
cMap=newMapFromValues("triggertime",triggertime,"name","Martin","phone","+486145558","info","Info");
addElement(cList, cMap);
cMap1=newMapFromValues("triggertime",triggertime,"name","Desmanto","phone","+626163451","info","Info1");
addElement(cList, cMap1);
js=toJSON(cList);
2nd method
Create only one cMap and create every key as List.

Code: Select all

cMap=newMapFromValues(
"triggertime", newList(),
"name", newList(),
"phone", newList(),
"info", newList());
Now when you need to add an entry do like this

Code: Select all

addElement(cMap['triggertime'], triggertime);
addElement(cMap['name'], "Martin");
addElement(cMap['phone'], "+486145558");
addElement(cMap['info'], "info");

js=toJSON(cMap)

Re: Maps and JSON tofile fromfile

Posted: 22 May 2019 11:22
by yogi108
Hi,

It seems to be the 2nd solution I prefer.

Thanks a lot,
Regards

[edit]
after some tests I am at the 1st solution now, got it working :-)

Re: Maps and JSON tofile fromfile

Posted: 25 May 2019 15:47
by Desmanto
First solution is better, as you always keep adding the map to the end of the list, no need to care about the key map (cMap1, cMap2, ....)

For 2nd solution, you can create the map as you do (cMap1), and then add the map.
cMap["cMap1"] = cMap1;

This will create the nested map with the key cMap1, cMap2, .... and so on. But might be quite confusing, so better solution 1.