0

I am pulling JSON output from the Open Movie Database linked here

In preparation for some analysis, I would like to re-order the Key:value pairs and write this output to a .txt file. The API gives me output in the following form:

{
 "Plot": "A skilled extractor is offered a chance to regain his old life as payment for a task considered to be impossible.", 
 "Rated": "PG-13", 
 "Response": "True", 
 "Language": "English, Japanese, French", 
 "Title": "Inception", 
 "Country": "USA, UK", 
 "Writer": "Christopher Nolan", 
 "Metascore": "74", 
 "imdbRating": "8.8", 
 "Director": "Christopher Nolan", 
 "Released": "16 Jul 2010", 
 "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy", 
 "Year": "2010", 
 "Genre": "Action, Adventure, Mystery", 
 "Awards": "Won 4 Oscars. Another 83 wins & 109 nominations.", 
 "Runtime": "148 min", 
 "Type": "movie", 
 "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg", 
 "imdbVotes": "912,048", 
 "imdbID": "tt1375666"
}

I would like to reorder the Key:Value pairs as such

Current      Desired structure
    Plot         Title
    Rated        Year
    Response     Rated
    Language     Released
    Title        Runtime
    Country      Genre
    Writer       Director
    Metascore    Writer
    imdbRating   Actors
    Director     Plot
    Released     Language
    Actors       Country
    Year         Awards
    Genre        Poster
    Awards       Metascore
    Runtime      IMDB rating
    Type         IMDB Votes
    Poster       IMDB_ID
    imdbVotes    Type
    imdbID       Response

Since the python json.loads() method turns the JSON schema into a dict, is there a simple/intuitive way to custom sort a new schema before I write this to my file, or will I need to write a for loop to write these key:value pairs into a new dict?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mike
  • 397
  • 5
  • 19
  • 4
    Dictionaries **have no order**, nor do JSON objects. You'll have to sort the keys yourself when writing data to a file, but there is little point in trying to write out the object as JSON again. – Martijn Pieters Feb 04 '14 at 15:49
  • I'm aware dictionaries have no order, I just didn't know if there was a "best practice" or even module for sorting JSON output. Surely I'm not the first person to want to re-format a JSON schema. – Mike Feb 04 '14 at 15:52
  • 1
    You are not the first to attempt it, no, not by far. There is just no point in trying, because JSON, as a standard, states that objects are unordered as well. Why do you need your keys ordered at all? – Martijn Pieters Feb 04 '14 at 15:53
  • As @MartijnPieters says, the order of Json properties is undefined (by designed). Out of interest, why would you care what order the Json is in? If you actually wanted to change the _structure_ (node hierarchy, etc...) then this would be possible by creating a new object from the old and then re-serializing to Json, but even then only the structure would be preserved, not the order. – Basic Feb 04 '14 at 15:53
  • Readability and personal preference, mostly. Maybe not the loftiest of goals, but if I'm going to be looking through the data, I'd like it to read in a way that makes sense to me. – Mike Feb 04 '14 at 15:55
  • @Mike: You can ask `json.dumps()` to sort keys (so you can produce stable output for tests or caching), but that's then writing keys in alphabetical order. You can muck about with `OrderedDict` objects to force a certain order. – Martijn Pieters Feb 04 '14 at 15:56

0 Answers0