1
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
  • 99,260
  • 22
  • 139
  • 249
KCReddy
  • 19
  • 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

2

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

vars = {'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(vars)
Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
  • `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,171
  • 5
  • 23
  • 39