1

I want to build json in following format

{
           "codeTableData":
           [
               {
               "codeValue": "11",
               "codeDisplayName": "AAAAAA"
               },
               {
               "codeValue": "22",
               "codeDisplayName": "BBBBBB"
               }
           ]
}

from tuple which is

result = [('11', 'AAAAAA'), ('22', 'BBBBBB'), ('33', 'CCCCCCCC')]

I have tried creating dictionary and then adding tuple data inside that dictionary but still no luck

jsonResult = {"codeTableData: " [
                              {
                                tmp1:tmp2
                              },
                          ]}
            json_data = json.dumps(jsonResult)

for above code program execution comes out of function with no error shown

3 Answers 3

1

You can construct a list of dictionaries with:

json_data = json.dumps(
    {'codeTableData':
     [{'codeValue': cv, 'codeDisplayName': cn} for cv, cn in result]}
)

We thus iterate over the list of 2-tuples, and for each item, we add a dictionary that maps 'codeValue' to the first item and codeDisplayName to the second item.

We can make use of zip as well:

column_names = ['codeValue', 'codeDisplayName']
json_data = json.dumps(
    {'codeTableData': [dict(zip(column_names, data)) for data in result]}
)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Wilem Van Onsem you saved my ass Its worked eaxctly as I wanted. I want to know is there any article which explains how to construct json dynamically in Python
@epoi: It has not that much to do with JSON itself, that is just the target encoding. It here uses list comprehension pythonforbeginners.com/basics/list-comprehensions-in-python to construct Python objects that are then converted to JSON.
Or look at the official python tutorial
1

Try code below, hope it will help.

results = [('11', 'AAAAAA'), ('22', 'BBBBBB'), ('33', 'CCCCCCCC')]

column_names = ['codeValue', 'codeDisplayName']

data = [ {key:data for key,data in zip(column_names,result) } for result in results ]

Ouput of this would be:

[{'codeValue': '11', 'codeDisplayName': 'AAAAAA'}, {'codeValue': '22', 'codeDisplayName': 'BBBBBB'}, {'codeValue': '33', 'codeDisplayName': 'CCCCCCCC'}]

For converting the data into json, as required use below code.

json.dumps({'code_data': [ {key:data for key,data in zip(column_names,result) } for result in results ]})

Now output would be:

{"code_data": [{"codeValue": "11", "codeDisplayName": "AAAAAA"}, {"codeValue": "22", "codeDisplayName": "BBBBBB"}, {"codeValue": "33", "codeDisplayName": "CCCCCCCC"}]}

Hope this will help.

Comments

1

you can do it just like:

result = [('11', 'AAAAAA'), ('22', 'BBBBBB'), ('33', 'CCCCCCCC')]

data_json = {"codeTableData":[]}

for item in result:
    data_json["codeTableData"].append({"codeValue":item[0],"codeDisplayName":item[1]})

print (data_json)

output:

{'codeTableData': [{'codeValue': '11', 'codeDisplayName': 'AAAAAA'}, {'codeValue': '22', 'codeDisplayName': 'BBBBBB'}, {'codeValue': '33', 'codeDisplayName': 'CCCCCCCC'}]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.