Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key Value Pair output
#4
the error is because you have extra opening bracket between dict and row.
if you need to get different json as body - construct the list of dicts

import json
rows = [(1, "Apple"), (2, "Banana"), (3, "Orange")]
json_rows = [dict(zip(('Id', 'Fruit'), (str(key), value))) for key, value in rows]
print(json.dumps(json_rows))
Output:
[{"Id": "1", "Fruit": "Apple"}, {"Id": "2", "Fruit": "Banana"}, {"Id": "3", "Fruit": "Orange"}] >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Key Value Pair output - by UtiliseIT - May-02-2019, 05:57 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:06 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 06:45 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:59 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 07:00 AM
RE: Key Value Pair output - by buran - May-02-2019, 07:02 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 04:41 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:06 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 07:20 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting the maximum value:key pair from a dictionary sean1 2 1,546 Jan-17-2022, 01:04 PM
Last Post: DeaD_EyE
  How to extract specific key value pair from string? aditi06 0 2,661 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Auto re-pair / re-sync Controller via Script? User3000 2 2,494 Nov-30-2020, 11:42 AM
Last Post: User3000
  team pair issue jk91 29 8,894 Mar-03-2020, 06:15 PM
Last Post: jefsummers
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 7,134 May-29-2019, 02:27 AM
Last Post: heiner55
  Parsing Text file having repeated value key pair using python manussnair 3 3,430 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020