title: Iterate over list of JSON objects
body: <p>I a have JSON file that contains a list likes this:</p>
  
  <pre><code>"locales": {
      "de": {
          "default": {
              "vegetables": [
                  "apple", 
                  "melon", 
                  "grape", 
                  "pear"
              ]
          }
      }, 
      "fr": {
          "default": {
              "vegetables": [
                  "apple", 
                  "melon", 
                  "grape", 
                  "pear"
              ]
          }
      }, 
      "nl": {
          "default": {
              "vegetables": [
                  "apple", 
                  "melon", 
                  "grape", 
                  "pear"
              ]
          }
      },
  (...)
  }
  </code></pre>
  
  <p>I would like to iterate over the <code>locales</code> ad manipulate <code>vegetables</code> depending on the locale (de, fr, nl etc.). How can I achieve that with python?</p>
  
  <p>I tried something like</p>
  
  <pre><code>import json 
  
  _MY_CUSTOM_ORDER_DE = [
      "pear",
      "grape",
      "apple",
      "melon"]
  
  _MY_CUSTOM_ORDER_NL = [
      "melon",
      "pear",
      "apple",
      "grape"]
  
  def updateJsonFile():
      jsonFile = open('vegetables.json', 'r') 
      data = json.load(jsonFile) 
      jsonFile.close() 
  
      for item in data["locales"]:
          if item == "de":
              item["default"]["vegetables"] = _MY_CUSTOM_ORDER_DE
          elif item == "nl":
              item["default"]["vegetables"] = _MY_CUSTOM_ORDER_NL
          else:
              ## do nothing ##
  
          with open('sortedvegetables.json', 'w') as outfile:
          json.dump(data, outfile, indent=4, sort_keys=True)
  
  def main():
      updateJsonFile()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Unfortunately this does not work but there is now error in terminal after I executed the script. <code>sortedvegetables.json</code> looks exactly the same like <code>vegetables.json</code> </p>