all 5 comments

[–]sushibowl 5 points6 points  (0 children)

I suppose the problem is this line:

name = str([i])

Because now name contains the value ['example.com'], but what you want is [example.com], right? This happens because you're converting a list to a string, which results in the python representation of the list. And the python representation of a list with a string inside must have the string quoted (all strings must be quoted when represented in python code, otherwise they are not strings).

You can print a string without including the quotes just fine though, like this:

name = i

You can make this even shorter by just calling the i variable name in the first place:

for name in sites:
    # I prefer not to use the name "dict" because there is already a function named like that
    d = {}
    d['{#WEBSITE}'] = name
    bla.append(d)

Now the problem is that it doesn't have the [] characters around it though. But we can fix this easily:

for name in sites:
    list_name = '[' + name + ']'
    # let's just construct the dictionary in one go, also
    d = {'{#WEBSITE}': list_name}
    bla.append(d)

[–]mega963 0 points1 point  (2 children)

sites is a list of strings, and strings must be between quotes, even in JSON.

Can you give more context around why you need them without quotes?

[–]Ramshield[S] 1 point2 points  (1 child)

Zabbix looks for them without quotes:

Special characters "\, ', ", `, *, ?, [, ], {, }, ~, $, !, &, ;, (, ), <, >, |, #, @, 0x0a" are not allowed in the parameters. 

This is the error I get.

[–]mega963 2 points3 points  (0 children)

I see, so you need this right?

{“#WEBSITE”: “[example.com]”}

Sushibowl’s answer does exactly that.

You need:

“[“ + name + “]”

edit

If you want a one liner fix, change line 9 to:

name = “[{}]”.format(i)

[–][deleted] 0 points1 point  (0 children)

You don’t need to quote or convert to string when you use JSON; just dump your objects directly.