user2916886
user2916886

Reputation: 845

how to combine data from 2 list of lists to form a single JSON in python

I have 2 csv files(semicolon delimited) with following structure and sample data:

File 1:

qid;question
1.0;How can I find specific content
2.0;How do I modify content
2.0;How can I edit items
2.0;I need to change some answers
3.0;How do I troubleshoot and fix problems

File 2:

qid;answer
1.0;Use the Filter feature to filter the items
2.0;Use the test tool to find your existing documents and edit them
3.0;Use the test tool to test a document

Now what I want to do is form a JSON structure which contains data from both documents to form following sample structure:

{
   "qna": [
      {
         "qid": "1.0",
         "q": [
            "How can I find specific content"
         ],
         "a": "Use the Filter feature to filter the items."

      },
      {
         "qid": "2.0",
         "q": [
            "How do I modify content","How do I edit items","I need to change some answers"
         ],
         "a": "Use the test tool to find your existing documents and edit them."
      },
      {
         "qid": "3.0",
         "q": [
            "How do I troubleshoot and fix problems"
         ],
         "a": "Use the test tool to test a document"

      }
]
}

As you can see what this JSON contains a list of list within qna field. Each of the items in this list of list contains the qid, list of questions matching same qid, and answer matching the qid.

I wrote this code where I am reading these 2 csv files and forming 2 list of lists:

qid_question_list = {}
qid_answer_list = {}

reader1 = csv.reader(csv_file1, delimiter=';')

next(reader1)

reader2 = csv.reader(csv_file2, delimiter=';')

next(reader2)

for qid,question in reader1:
    if qid not in qid_question_list:
        qid_question_list[qid] = list()
    qid_question_list[qid].append(question)

for qid,answer in reader2:
    if qid not in qid_answer_list:
        qid_answer_list[qid] = list()
    qid_answer_list[qid].append(answer)

When I execute above I get following 2 lists:

qid_question_list

{'1.0': ['How can I find specific content'], '2.0': ['How do I modify content','How do I edit items','I need to change some answers'], '3.0': ['How do I troubleshoot and fix problems']}

qid_answer_list

{'1.0': ['Use the Filter feature to filter the items'], '2.0': ['Use the test tool to find your existing documents and edit them'], '3.0': ['Use the test tool to test a document']}

Now I am not able to determine how can I club these 2 list of lists to form the desired JSON structure shown above?

Upvotes: 1

Views: 68

Answers (1)

Van Peer
Van Peer

Reputation: 2167

Assuming you've the two dicts handy

l1={'1.0': ['How can I find specific content'], '2.0': ['How do I modify content','How do I edit items','I need to change some answers'], '3.0': ['How do I troubleshoot and fix problems']}

l2={'1.0': ['Use the Filter feature to filter the items'], '2.0': ['Use the test tool to find your existing documents and edit them'], '3.0': ['Use the test tool to test a document']}

l = [ {"qid": k, "q": v, "a": l2[k]} for k,v in l1.items() ]

q={'qna': l }

print(q)

Output

{'qna': [{'qid': '1.0', 'q': ['How can I find specific content'], 'a': ['Use the Filter feature to filter the items']}, {'qid': '2.0', 'q': ['How do I modify content', 'How do I edit items', 'I need to change some answers'], 'a': ['Use the test tool to find your existing documents and edit them']}, {'qid': '3.0', 'q': ['How do I troubleshoot and fix problems'], 'a': ['Use the test tool to test a document']}]}

Upvotes: 3

Related Questions