Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

Is converting multiple nested python dictionaries/lists into json going to cause problems if other languages try to decode them?

An aim of a research project I am doing using python is that my results are as accessible as possible. As a result, I am converting all my results to JSON so that future researchers who might want to use different languages can access and manipulate my data easily. At present, I am storing the data (YouTube comments data) in Python as multiple nested dictionaries/lists and then writing it to a JSON file:

import requests
import json

params = {
    'part': 'snippet,replies',
    'maxResults': 100,
    'videoId': ********,
    'textFormat': 'plainText',
    'key': ***********
    }

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    }

comments_data = requests.get('https://www.googleapis.com/youtube/v3/commentThreads', params=params, headers=headers)
results = comments_data.json()

comments_list = []

for item in results["items"]:           
    comment = item["snippet"]["topLevelComment"]      
    author = comment['snippet']["authorDisplayName"]     
    text = comment['snippet']["textDisplay"]
    likes = comment['snippet']["likeCount"]
    reply_count = item['snippet']['totalReplyCount']

    comments_list.append({    #appends comment as a dictionary
        "author" : author,
        "text": text,
        "likes": likes,
        "reply_count": reply_count
            })

     if 'replies' in item.keys():     #checks if a comment has any replies
         comments_list[-1].update({"replies":[]})
         for reply in item['replies']["comments"]:
             rep_author = reply['snippet']["authorDisplayName"]     
             rep_text = reply['snippet']["textDisplay"]
             rep_likes = reply['snippet']["likeCount"]

             comments_list[-1]['replies'].append({    #appends replies to latest entry in comments_list (aka its parent)
                 "author" : rep_author,
                 "text": rep_text,
                 "likes": rep_likes
                 })

with open("comments.json, "w", encoding="utf8") as f:
    json.dump(comments_list, f, ensure_ascii=False)

As you can see, I am storing information about the comments, in particular the replies, as nested dictionaries.

I only have experience with Python, so I am unsure if other languages would struggle to process this data or not. Can anyone shed some light?

question from:https://stackoverflow.com/questions/65900676/is-converting-multiple-nested-python-dictionaries-lists-into-json-going-to-cause

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Standard JSON can be decoded without problem if all you're nesting is lists/dicts/quotes. You already did this, but it's worth mentioning that the str -> bytes encoding should be clear("utf-8" in most cases).

Side note: do fix any typos in the code though :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...