Home > Net >  Reverse a JSON file
Reverse a JSON file

Time:11-04

I have a JSON file with mime types like this:

{
    "video/quicktime": ["mov", "qt"],
    "video/vnd.dece.hd": ["uvh", "uvvh"],
    "video/vnd.dece.mobile": ["uvm", "uvvm"],
    "video/vnd.dece.pd": ["uvp", "uvvp"],
    "video/vnd.dece.sd": ["uvs", "uvvs"],
    "video/vnd.dece.video": ["uvv", "uvvv"],
    "video/vnd.dvb.file": ["dvb"],
    "video/vnd.fvt": ["fvt"],
    "video/vnd.mpegurl": ["m4u", "mxu"],
    "video/vnd.ms-playready.media.pyv": ["pyv"],
    "video/vnd.uvvu.mp4": ["uvu", "uvvu"],
}

it goes on for like 800 lines, so by hand is not a choice.

I now need to reverse this array to

{
    "mov": "video/quicktime",
    "qt": "video/quicktime",
    "uvh": "video/dece.hd"

}

and so on.

Can I do this?

CodePudding user response:

Try this little program in C#:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace ReverseJson
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonText = @"{
                    'video/quicktime': ['mov', 'qt'],
                    'video/vnd.dece.hd': ['uvh', 'uvvh'],
                    'video/vnd.dece.mobile': ['uvm', 'uvvm'],
                    'video/vnd.dece.pd': ['uvp', 'uvvp'],
                    'video/vnd.dece.sd': ['uvs', 'uvvs'],
                    'video/vnd.dece.video': ['uvv', 'uvvv'],
                    'video/vnd.dvb.file': ['dvb'],
                    'video/vnd.fvt': ['fvt'],
                    'video/vnd.mpegurl': ['m4u', 'mxu'],
                    'video/vnd.ms-playready.media.pyv': ['pyv'],
                    'video/vnd.uvvu.mp4': ['uvu', 'uvvu'],
                }";
            
            var json = JsonConvert.DeserializeObject<dynamic>(jsonText);

            var dict = new Dictionary<string, string>();
            foreach (var item in json)
            {
                string media = item.Name;
                foreach (var extension in item.Value)
                {
                    string ext = extension.Value;
                    if (!dict.ContainsKey(ext))
                    {
                        dict.Add(ext, media);
                    }
                }
            }

            Console.Write(JsonConvert.SerializeObject(dict,Formatting.Indented));
        }
    }
}

OUTPUT:

{
  "mov": "video/quicktime",
  "qt": "video/quicktime",
  "uvh": "video/vnd.dece.hd",
  "uvvh": "video/vnd.dece.hd",
  "uvm": "video/vnd.dece.mobile",
  "uvvm": "video/vnd.dece.mobile",
  "uvp": "video/vnd.dece.pd",
  "uvvp": "video/vnd.dece.pd",
  "uvs": "video/vnd.dece.sd",
  "uvvs": "video/vnd.dece.sd",
  "uvv": "video/vnd.dece.video",
  "uvvv": "video/vnd.dece.video",
  "dvb": "video/vnd.dvb.file",
  "fvt": "video/vnd.fvt",
  "m4u": "video/vnd.mpegurl",
  "mxu": "video/vnd.mpegurl",
  "pyv": "video/vnd.ms-playready.media.pyv",
  "uvu": "video/vnd.uvvu.mp4",
  "uvvu": "video/vnd.uvvu.mp4"
}

A word of caution: this only works if no extension is associated with more than one media type. This is implied by the specified output format in the question.

  •  Tags:  
  • json
  • Related