Home > database >  How to rename JSON File in bulk?
How to rename JSON File in bulk?

Time:10-05

First of all, i am new in JSON and Python Code. so sorry if the question is noob.

i've some code on Python Code like this :

import os
import json

def extract_video_data_from_url(url):
    
    
    command = f'yt-dlp "{url}" -j --no-playlist'
    output = os.popen(command).read()
    print(output)
    video_data = json.loads(output)
    title = video_data["title"]
    formats = video_data["formats"]
    thumbnail = video_data["thumbnail"]
    formats = [extract_format_data(format_data) for format_data in formats]
    return {
        "title": title,
        "formats": formats,
        "thumbnail": thumbnail
    }

and have json output file like this :

"formats": [
    {
      "extension": "webm",
      "format_name": "249-0 - audio only (tiny)",
      "url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=o-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=249&source=youtube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&xtags=drc=1&mime=audio/webm&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=5717592&dur=823.441&lmt=1658621716740625&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&txp=1311224&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,xtags,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRQIgJJPzkh0yEr857SAAf-uj8yaXUWMPgWY4D26wq-c8_aMCIQDCojLFA8Si9VTutJBJMI8KV34cFarhmkYueIYsXULV3A==&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="
    },
    {
      "extension": "webm",
      "format_name": "249-1 - audio only (tiny)",
      "url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=o-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=249&source=youtube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&mime=audio/webm&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=5748345&dur=823.421&lmt=1507047911229800&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRAIgYVyc5YXxtlZsreXTXt9-jJkVXk0Hk4CChtG9wWPENzsCIHqzMvso2PqOQe-n5duG6gq_bEnRiFMH2q4BFAgfY7ZN&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="
    },
    {
      "extension": "webm",
      "format_name": "250-0 - audio only (tiny)",
      "url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=o-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=250&source=youtube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&xtags=drc=1&mime=audio/webm&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=7625449&dur=823.441&lmt=1658621488559767&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&txp=1311224&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,xtags,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRQIhALSnuGkzu4QS9HOVaMHM73q8qmNHfjpOwznkf7jbuzM4AiAOZkgQQCo3PZdHEgc2IugYNpxtIpKElEd8jlWH8waLEg==&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="
    },
    {
      "extension": "webm",
      "format_name": "250-1 - audio only (tiny)",
      "url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=o-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=250&source=youtube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&mime=audio/webm&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=7663003&dur=823.421&lmt=1507047925961430&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRQIhAKIfh01EuN0tRq0YpTjdsm6HpI3pNzK_CVAsCm_Qub-6AiABSRoHwImFd6cXJ91gznDA6RoBtL4uxQ6JP-sE9_vkMA==&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="
    },
    {
      "extension": "m4a",
      "format_name": "140-0 - audio only (tiny)",
      "url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=o-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=140&source=youtube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&mime=audio/mp4&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=13079535&dur=823.472&lmt=1382391261349662&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRQIgBxYlJFbwPupCcNu-3wcjmLawPUA2Am_klM5PzPvwD38CIQCKcoEW9WT4LuiUTy1rqp_KN7t_xwsrHA7fuX7WjItvdA==&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="
    },

how do i change name field in format_name at json's file to be rename let say like "MP3" or "MP4" in bulk?

Thank in advance.

CodePudding user response:

import json 

data = '{"formats": [{"extension": "webm", "format_name": "249-0 - audio only (tiny)","url": "https://rr3---sn-npoe7nlz.googlevideo.com/videoplayback?expire=1664538191&ei=74E2Y5-mH5Sl4-EP35CdyAM&ip=180.243.7.35&id=-AAmP2xRT8Ve3XqKtlFWcogCjzcmOHYaCLBTvtRA-f6yV&itag=249&sourceyoutube&requiressl=yes&mh=gc&mm=31,29&mn=sn-npoe7nlz,sn-npoeenly&ms=au,rdu&mv=m&mvi=3&pl=24&initcwndbps=447500&vprv=1&xtags=drc=1&mime=audio/webm&ns=wIsmeet1EyLH2WlcZc1VlvEI&gir=yes&clen=5717592&dur=823.441&lmt=1658621716740625&mt=1664516251&fvip=3&keepalive=yes&fexp=24001373,24007246&c=WEB&txp=1311224&n=rewrNpMiP3pdRF8H&sparams=expire,ei,ip,id,itag,source,requiressl,vprv,xtags,mime,ns,gir,clen,dur,lmt&sig=AOq0QJ8wRQIgJJPzkh0yEr857SAAf-uj8yaXUWMPgWY4D26wq-c8_aMCIQDCojLFA8Si9VTutJBJMI8KV34cFarhmkYueIYsXULV3A==&lsparams=mh,mm,mn,ms,mv,mvi,pl,initcwndbps&lsig=AG3C_xAwRQIhAO7zrkQ_e2V_tBVx8qTw6o0zPhnR86gEgH1aMwu3UTTTAiAO4wwWZ6BFnr6k_xUqKtUd10SbChPqE8aqGJHbHb-azw=="} ]} '

data = json.loads(data)["formats"]
for element in data:
    if "audio only" in element["format_name"]:
        element["format_name"] = "MP3"
print(data)

EDIT: You can add other test for "video only" for example.

  • Related