2
ip_adrs_ve100_lst = ['5.5.5.1', '5.5.5.2']
at_ip_1 = '10.10.10.1'
at_ip_2 = '10.10.10.2'

to json format.

martineau
  • 119,623
  • 25
  • 170
  • 301
KCReddy
  • 29
  • 1
  • 1
  • 3
  • 3
    Possible duplicate of [Serializing python object instance to JSON](http://stackoverflow.com/questions/10252010/serializing-python-object-instance-to-json) – Lukas Körfer Feb 01 '17 at 11:53

2 Answers2

3

Just create a dictionary, and then convert it to json:

data_dict = {'ip_adrs_ve100_lst': ['5.5.5.1', '5.5.5.2'],
        'at_ip_1': '10.10.10.1',
        'at_ip_2': '10.10.10.2'}
import json
to_json = json.dumps(data_dict)
Sravan Kumar
  • 1,447
  • 3
  • 19
  • 41
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    `vars` is the name of a Python built-in function, so creating a variable with the same name is a poor programming practice and ill-advised. – martineau Feb 01 '17 at 12:08
1

Just to mention, add ensure_ascii=False to json.dumps() call to ensure that the returned instance is always unicode:

import json

my_dict = {
    'ip_adrs_ve100_lst': ['5.5.5.1', '5.5.5.2'],
    'at_ip_1': '10.10.10.1',
    'at_ip_2': '10.10.10.2'
}
my_json = json.dumps(my_dict, ensure_ascii=False)
Huy Vo
  • 2,418
  • 6
  • 22
  • 43