Mayur Shinde
Mayur Shinde

Reputation: 55

Converting 2 array objects as key - value pair in JSON object using PYTHON

I have 2 different arrays:

dataList = ['a_cout', 'b_count', 'c_count']
dataList1 = [15404, 21381, 3]

I am trying to merge them into a json object as Key-Value pair like :

'{
  "a_count" : 15401,
  "b_count" : 21381,
  "c_count" : 3
 }'

I am using json lib in Python 2.x

Upvotes: 0

Views: 1011

Answers (1)

jeremy_rutman
jeremy_rutman

Reputation: 5788

You can get those lists into a dictionary with a comprehension, then jsonify the dictionary:

import json 
mydict = {k:v for k,v in zip(dataList,dataList1)}
jdict = json.dumps(mydict)

Upvotes: 3

Related Questions