0

I am trying to make a json file as I am new in python the json file i have is as followed and i want to reformat it

{  
   "A1":"a1",
   "aback":"\u0259b\u00e6k",
   "abaft":"abaft",
   "abandon":"\u0259b\u00e6nd\u0259n",
   "abandoned":"\u0259b\u00e6nd\u0259nd",
   "abandonment":"\u0259b\u00e6nd\u0259nm\u0259nt",
   "abase":"abase",
   "abash":"\u0259b\u00e6\u0283",
   "abashment":"abashment",
   "abate":"\u0259bet",
   "abatement":"\u0259betm\u0259nt",
   "abbey":"\u00e6bi",
   "abbreviate":"\u0259briviet",
   "abbreviation":"\u0259brivie\u0283\u0259n"
}

the format that i want to achieve is

{  
   word: "A1",
   transcription:"a1"
}
{
   word: "aback",
   transcription :"\u0259b\u00e6k"
}
{
  word:"abaft"
  transcrition:"abaft"
}
{
  word:"abbreviation",
  transcription:"\u0259brivie\u0283\u0259n"
}
2
  • 2
    The expected format is not valid json. What do you want it to be? A dictionary? Or a json? Also, what have you tried? Commented Apr 1, 2017 at 9:03
  • Do you want to retain alphabetical order? FWIW, your input format is more useful than your output format because dictionary lookup is O(1) but list lookup is O(n). Commented Apr 1, 2017 at 9:19

3 Answers 3

2

Read the JSON from the file and convert it to a dictionary. Use a list comprehension to generate a list of dictionaries and write that out as a JSON list:

import json

with open('file.json') as infile, open('out.json', 'w') as outfile:
    d = json.load(infile)
    json.dump([{'word': k, 'transcription': d[k]} for k in d], outfile)

For your input file this will produce an output file containing:

[{"transcription": "\u0259b\u00e6\u0283", "word": "abash"}, {"transcription": "\u0259briviet", "word": "abbreviate"}, {"transcription": "abaft", "word": "abaft"}, {"transcription": "a1", "word": "A1"}, {"transcription": "\u0259b\u00e6nd\u0259nd", "word": "abandoned"}, {"transcription": "\u0259b\u00e6nd\u0259nm\u0259nt", "word": "abandonment"}, {"transcription": "\u0259betm\u0259nt", "word": "abatement"}, {"transcription": "\u0259bet", "word": "abate"}, {"transcription": "\u0259b\u00e6nd\u0259n", "word": "abandon"}, {"transcription": "\u00e6bi", "word": "abbey"}, {"transcription": "\u0259brivie\u0283\u0259n", "word": "abbreviation"}, {"transcription": "\u0259b\u00e6k", "word": "aback"}, {"transcription": "abase", "word": "abase"}, {"transcription": "abashment", "word": "abashment"}]

You can format the output a bit, use indent:

json.dump([{'word': k, 'transcription': d[k]} for k in d], outfile, indent='')

will output:

[
    {
        "word": "aback",
        "transcription": "\u0259b\u00e6k"
    },
    {
        "word": "abandonment",
        "transcription": "\u0259b\u00e6nd\u0259nm\u0259nt"
    },
    {
        "word": "abatement",
        "transcription": "\u0259betm\u0259nt"
    },
    {
        "word": "abbey",
        "transcription": "\u00e6bi"
    },
    {
        "word": "abbreviation",
        "transcription": "\u0259brivie\u0283\u0259n"
    },
    {
        "word": "abandoned",
        "transcription": "\u0259b\u00e6nd\u0259nd"
    },
    {
        "word": "abash",
        "transcription": "\u0259b\u00e6\u0283"
    },
    {
        "word": "abaft",
        "transcription": "abaft"
    },
    {
        "word": "abashment",
        "transcription": "abashment"
    },
    {
        "word": "abate",
        "transcription": "\u0259bet"
    },
    {
        "word": "abbreviate",
        "transcription": "\u0259briviet"
    },
    {
        "word": "A1",
        "transcription": "a1"
    },
    {
        "word": "abandon",
        "transcription": "\u0259b\u00e6nd\u0259n"
    },
    {
        "word": "abase",
        "transcription": "abase"
    }
]

Note that your requested format is not actually valid JSON. If you do not want the commas and list brackets you can write the file out like this:

with open('file.json') as infile, open('out.json', 'w') as outfile:
    d = json.load(infile)
    print(*[json.dumps({'word': k, 'transcription': d[k]}, indent='    ') for k in d], file=outfile, sep='\n')
{
    "word": "aback",
    "transcription": "\u0259b\u00e6k"
}
{
    "word": "abandonment",
    "transcription": "\u0259b\u00e6nd\u0259nm\u0259nt"
}
{
    "word": "abatement",
    "transcription": "\u0259betm\u0259nt"
}
{
    "word": "abbey",
    "transcription": "\u00e6bi"
}
{
    "word": "abbreviation",
    "transcription": "\u0259brivie\u0283\u0259n"
}
{
    "word": "abandoned",
    "transcription": "\u0259b\u00e6nd\u0259nd"
}
{
    "word": "abash",
    "transcription": "\u0259b\u00e6\u0283"
}
{
    "word": "abaft",
    "transcription": "abaft"
}
{
    "word": "abashment",
    "transcription": "abashment"
}
{
    "word": "abate",
    "transcription": "\u0259bet"
}
{
    "word": "abbreviate",
    "transcription": "\u0259briviet"
}
{
    "word": "A1",
    "transcription": "a1"
}
{
    "word": "abandon",
    "transcription": "\u0259b\u00e6nd\u0259n"
}
{
    "word": "abase",
    "transcription": "abase"
}

Finally, if the order is important you can iterate over the sorted keys, just use:

for k in sorted(d)

where appropriate.

Sign up to request clarification or add additional context in comments.

Comments

0

Copy this code to a file such as convert.py

import sys
import json
from pprint import PrettyPrinter, pprint


def main():
    fn =   sys.argv[1]
    with open(fn, 'rb') as f:
        data = json.loads(f.read())

    for k, v in data.items():
        pprint({'word':k, 'transcription':v})


if __name__ == '__main__':
    main()

and then run command python convert.py YOUR_JSON_FILE_NAME, enjoy it ~

Comments

0
In [16]: d
Out[16]: {'a': 'A', 'b': 'B', 'c': 'C'}

In [17]: lis = []

In [18]: lis
Out[18]: []

In [19]: for key in d:
    ...:     dd ={}
    ...:     dd["word"] = key
    ...:     dd["transcrition"] = d[key]
    ...:     lis.append(dd)
    ...:     

In [20]: lis
Out[20]: 
[{'transcrition': 'A', 'word': 'a'},
 {'transcrition': 'C', 'word': 'c'},
 {'transcrition': 'B', 'word': 'b'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.