I tried to parse data from elixir map converted to JSON using Poison.encode!(), in any case I got error:
Unexpected token & in JSON at position 1
How to escape double quotes inside this string, to prevent adding ""e;"?
Initial data:
%{"[email protected]" => ["40000000-5fffffff", "a0000000-bfffffff"],
"[email protected]" => ["20000000-3fffffff", "a0000000-bfffffff"],
"[email protected]" => ["20000000-3fffffff", "80000000-9fffffff"],
"[email protected]" => ["20000000-3fffffff", "80000000-9fffffff",
"e0000000-ffffffff"],
"[email protected]" => ["00000000-1fffffff", "80000000-9fffffff",
"e0000000-ffffffff"],
"[email protected]" => ["00000000-1fffffff", "60000000-7fffffff",
"e0000000-ffffffff"],
"[email protected]" => ["00000000-1fffffff", "60000000-7fffffff",
"c0000000-dfffffff"],
"[email protected]" => ["60000000-7fffffff", "c0000000-dfffffff"],
"[email protected]" => ["40000000-5fffffff", "c0000000-dfffffff"],
"[email protected]" => ["40000000-5fffffff", "a0000000-bfffffff"]}
Mapped to new structure, using this method inside controller:
defp map_shards(nodes, :nodes), do: %{"nodes": Enum.map(nodes, fn{k, v} -> %{"node": k, "shards": v} end)}
Call inside index action:
nodes = map_shards(resp["by_node"], :nodes)
Now I have map as this:
%{nodes: [%{node: "[email protected]", shards: ["40000000-5fffffff", "a0000000-bfffffff"]}, %{node: "[email protected]", shards: ["20000000-3fffffff", "a0000000-bfffffff"]}, %{node: "[email protected]", shards: ["20000000-3fffffff", "80000000-9fffffff"]}, %{node: "[email protected]", shards: ["20000000-3fffffff", "80000000-9fffffff", "e0000000-ffffffff"]}, %{node: "[email protected]", shards: ["00000000-1fffffff", "80000000-9fffffff", "e0000000-ffffffff"]}, %{node: "[email protected]", shards: ["00000000-1fffffff", "60000000-7fffffff", "e0000000-ffffffff"]}, %{node: "[email protected]", shards: ["00000000-1fffffff", "60000000-7fffffff", "c0000000-dfffffff"]}, %{node: "[email protected]", shards: ["60000000-7fffffff", "c0000000-dfffffff"]}, %{node: "[email protected]", shards: ["40000000-5fffffff", "c0000000-dfffffff"]}, %{node: "[email protected]", shards: ["40000000-5fffffff", "a0000000-bfffffff"]}]}
JS code, with parser call inside template .eex:
var theData = ("<%= Poison.Encoder.encode(@nodes, []) %>");
theData = JSON.parse(theData);
What I see in JS console:
var theData = ("{"nodes":[{"shards":["40000000-5fffffff","a0000000-bfffffff"],"node":"[email protected]"},{"shards":["20000000-3fffffff","a0000000-bfffffff"],"node":"[email protected]"},{"shards":["20000000-3fffffff","80000000-9fffffff"],"node":"[email protected]"},{"shards":["20000000-3fffffff","80000000-9fffffff","e0000000-ffffffff"],"node":"[email protected]"},{"shards":["00000000-1fffffff","80000000-9fffffff","e0000000-ffffffff"],"node":"[email protected]"},{"shards":["00000000-1fffffff","60000000-7fffffff","e0000000-ffffffff"],"node":"[email protected]"},{"shards":["00000000-1fffffff","60000000-7fffffff","c0000000-dfffffff"],"node":"[email protected]"},{"shards":["60000000-7fffffff","c0000000-dfffffff"],"node":"[email protected]"},{"shards":["40000000-5fffffff","c0000000-dfffffff"],"node":"[email protected]"},{"shards":["40000000-5fffffff","a0000000-bfffffff"],"node":"[email protected]"}]}");
theData = JSON.parse(theData);
You need to disable the automatic HTML escaping when injecting encoded JSON like that. You also don't need the surrounding "
or brackets.
var theData = <%= raw Poison.Encoder.encode(@nodes, []) %>;
There's no need to call JSON.parse
anymore, theData
will be set to the value itself, not a string containing a JSON.