A very quick article on converting a Map string into a HTML table without using a for each loop.
Why?
I have quite a big response from our CRM that hits a statement execution limit if I use a for loop. I have a map with 3 columns: first_name, last_name, and ID. What I want is a HTML table now with the headers and data but without using a for loop.
What I have:
{"First_Name":"Joel","Last_Name":"Lipman","id":"1"},{"First_Name":"Another","Last_Name":"Person","id":"2"}
- {"First_Name":"Joel","Last_Name":"Lipman","id":"1"},{"First_Name":"Another","Last_Name":"Person","id":"2"}
First Name | Last Name | ID |
---|---|---|
Joel | Lipman | 1 |
Another | Person | 2 |
How?
This is a bit of a dirty solution and as long as "id" is not your first column (because "id" can be found in names like "David") it will work.
Assume a Map specified in deluge:
l_Data = List(); m_Data = Map(); m_Data.put("First_Name","Joel"); m_Data.put("Last_Name","Lipman"); m_Data.put("id","1"); l_Data.add(m_Data); m_Data = Map(); m_Data.put("First_Name","Another"); m_Data.put("Last_Name","Person"); m_Data.put("id","2"); l_Data.add(m_Data); v_Data = l_Data.toString(); info v_Data; // yields // {"First_Name":"Joel","Last_Name":"Lipman","id":"1"},{"First_Name":"Another","Last_Name":"Person","id":"2"}
- l_Data = List();
- m_Data = Map();
- m_Data.put("First_Name","Joel");
- m_Data.put("Last_Name","Lipman");
- m_Data.put("id","1");
- l_Data.add(m_Data);
- m_Data = Map();
- m_Data.put("First_Name","Another");
- m_Data.put("Last_Name","Person");
- m_Data.put("id","2");
- l_Data.add(m_Data);
- v_Data = l_Data.toString();
- info v_Data;
- // yields
- // {"First_Name":"Joel","Last_Name":"Lipman","id":"1"},{"First_Name":"Another","Last_Name":"Person","id":"2"}
Some formatting and a lot of replaceAll:
l_Keys = List(); l_Keys = v_Data.toMap().keys(); v_DataHtml = v_Data.replaceAll("},{", "}{", true); v_DataHtml = v_DataHtml.replaceAll(l_Keys.get(0) + "\"", "<td>", true); v_DataHtml = v_DataHtml.replaceAll("\",\"" + l_Keys.get(1) + "\"", "<td>", true); v_DataHtml = v_DataHtml.replaceAll("\",\"" + l_Keys.get(2) + "\"", "</td><td>", true); // tidy up v_DataHtml = v_DataHtml.replaceAll("{", "<tr>", true); v_DataHtml = v_DataHtml.replaceAll("\"}", "</td></tr>\n", true); v_DataHtml = v_DataHtml.replaceAll(":\"", "", true); v_DataHtml = v_DataHtml.replaceAll(">\"", ">", true); v_DataHtml = v_DataHtml.replaceAll("\"<", "<", true); info "<table>\n" + v_DataHtml + "</table>";
- l_Keys = List();
- l_Keys = v_Data.toMap().keys();
- v_DataHtml = v_Data.replaceAll("},{", "}{", true);
- v_DataHtml = v_DataHtml.replaceAll(l_Keys.get(0) + "\"", "<td>", true);
- v_DataHtml = v_DataHtml.replaceAll("\",\"" + l_Keys.get(1) + "\"", "<td>", true);
- v_DataHtml = v_DataHtml.replaceAll("\",\"" + l_Keys.get(2) + "\"", "</td><td>", true);
- // tidy up
- v_DataHtml = v_DataHtml.replaceAll("{", "<tr>", true);
- v_DataHtml = v_DataHtml.replaceAll("\"}", "</td></tr>\n", true);
- v_DataHtml = v_DataHtml.replaceAll(":\"", "", true);
- v_DataHtml = v_DataHtml.replaceAll(">\"", ">", true);
- v_DataHtml = v_DataHtml.replaceAll("\"<", "<", true);
- info "<table>\n" + v_DataHtml + "</table>";