title: PYTHON convert multidimensionale list to JSON data
body: <p>I'm struggling to bring my array to the right format for the JSON file! Might be an easy question for people with knowledge.
  I grep the data from different CSV files in the format:</p>
  <p><code> [['Dec', '196610'], ['Oct', '196699'], ['Sep', '131073'], ['Jul', '122050']]</code></p>
  <p>I need to bring it in the format:</p>
  <pre><code>{
      &quot;Year_2021&quot;: [
          {
              &quot;Dec&quot;: &quot;196610&quot;,
              &quot;oct&quot;: &quot;196699&quot;,
              &quot;Sep&quot;: &quot;131073&quot;,
              &quot;Jul&quot;: &quot;122050&quot;
          }
      ]
  }
  </code></pre>
  <p>i have tried different possibilities and got the craziest constellations, just not the right one.</p>
  <p>My current code:</p>
  <pre><code>import csv
  import datetime
  import time
  import os.path
  import json
  from collections import defaultdict
  
  sys_time = datetime.datetime.now()
  format_sys_time = sys_time.strftime('[%Y-%m-%d] %H:%M:%S')      # Other Datetime Format
  
  act_month = int(sys_time.strftime('%m'))
  #print act_month
  
  output_json_file    = &quot;/var/www/json/EXP_MONTHLY_POWER_&quot; + sys_time.strftime('%Y') + &quot;.json&quot;
  
  start_time = time.time()    # measure how long a programm run takes
  
  columns = defaultdict(list) # each value in each column is appended to a list
  
  # Help function to build dword
  def word_to_dword(val_1, val_2):
      result = val_1
      result |= val_2 &lt;&lt; 16
      return result
  
  # Calculate past days of the current month
  dates = []
  for act_month in range(act_month+1, 1, -1):
      act_month -= 1
      if act_month &lt;=9:
       INPUTFILE= &quot;/var/www/csv/EXP_POWER_&quot; + sys_time.strftime('%Y-0') + str(act_month) + &quot;.csv&quot;
      else:
       INPUTFILE= &quot;/var/www/csv/EXP_POWER_&quot; + sys_time.strftime('%Y-') + str(act_month) + &quot;.csv&quot;
      dates.append(INPUTFILE)
  
  #print(dates)
  outputfields = [];
  
  #Delete old file to avoid double entrys
  if os.path.exists(output_json_file):
      os.remove(output_json_file)
  
  
  for dayfile in dates:
      if os.path.exists(dayfile):
          with open(dayfile) as csvdatei:
              mvg_reader = csv.DictReader(csvdatei , delimiter=';')   # read rows into a dictionary format
              for row in mvg_reader:                                  # read a row as {column1: value1, column2: value2,...}
                  for (fieldname, value) in row.items():              # go over each column name and value
                      columns[fieldname].append(value)                # append the value into the appropriate list
                                                                      # based on column name
  
          outfields1=[]
  
          for value in columns['Power_Value(kwh)']:
          #    print(int(value))
              outfields1.append(int(value))
  
          # Build Power Value from last values of list
          if (len(outfields1) != 0):
              DL_Actual_Power_Value = outfields1[-1]
          else:
              print(&quot;Keine Daten gefunden!&quot;)
  
          compString = &quot;&quot;
          compString = dayfile[28:30]     # Build compString from Filename
          
          switcher = {
                  &quot;01&quot;: &quot;Jan&quot;,
                  &quot;02&quot;: &quot;Feb&quot;,
                  &quot;03&quot;: &quot;Mar&quot;,
                  &quot;04&quot;: &quot;Apr&quot;,
                  &quot;05&quot;: &quot;May&quot;,
                  &quot;06&quot;: &quot;Jun&quot;,
                  &quot;07&quot;: &quot;Jul&quot;,
                  &quot;08&quot;: &quot;Aug&quot;,
                  &quot;09&quot;: &quot;Sep&quot;,
                  &quot;10&quot;: &quot;Oct&quot;,
                  &quot;11&quot;: &quot;Nov&quot;,
                  &quot;12&quot;: &quot;Dec&quot;
              }        
              
          outputfields.append([switcher.get(compString, &quot;Invalid month&quot;), str(DL_Actual_Power_Value)]       )
               
  print(outputfields)
  
  
      
  
  export_json_main = {}
  export_json_main.clear()
  export_KEY = 'Year_' + sys_time.strftime('%Y')      # Name des Keys festlegen 
  export_json_main[export_KEY] = []
  export_json_main[export_KEY].append(      outputfields    )
  
  
      
  #jsonStr = json.dumps(outputfields)
  #print(jsonStr)
  #for keys in (outputfields):
  #    print(keys[0])
  #export_json_main[export_KEY].append({
  #    json.dumps(outputfields)
  #})
  with open(output_json_file, 'w') as outfile:
      json.dump(export_json_main, outfile, indent=4)
  </code></pre>
  <p>This brings me the following output:</p>
  <pre><code>{
      &quot;Year_2021&quot;: [
          [
              [
                  &quot;Dec&quot;, 
                  &quot;196610&quot;
              ], 
              [
                  &quot;Oct&quot;, 
                  &quot;196699&quot;
              ], 
              [
                  &quot;Sep&quot;, 
                  &quot;131073&quot;
              ], 
              [
                  &quot;Jul&quot;, 
                  &quot;122050&quot;
              ]
          ]
      ]
  }
  </code></pre>