KCReddy
KCReddy

Reputation: 29

How to convert variables to json?

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.

Upvotes: 2

Views: 12429

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

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)

Upvotes: 3

Huy Vo
Huy Vo

Reputation: 2500

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)

Upvotes: 1

Related Questions