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
  • 112,593
  • 23
  • 157
  • 280
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:

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,407
  • 3
  • 19
  • 40
Burhan Khalid
  • 161,711
  • 18
  • 231
  • 272
  • 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,318
  • 6
  • 26
  • 43