title: writing a nested json into a csv using python
body: <p>I have a file with the following json format in python:</p>
  
  <pre><code>{"header":{"a":"1","b":"1"}, 
  
  "data":[{"a":"1", "b":{"ba":"b1","bb":"b2","bc":"b3"}, "c":{"ca":"x1","cb":"x2","cc":"x3"}, "d":"4"}, 
  
          {"a":"12", "b":{"ba":"12a","bb":"12ab","bc":"1ab"},"c":{"ca":"12z","cb":"12zz","cc":"12zzz"}, "d":"12"}
         ]}
  </code></pre>
  
  <p>I've written a csv parser without the nested 'b' and 'c' element, but am having difficulties trying to parse selected elements from 'b' and 'c' to my csv. Here's what I have so far:</p>
  
  <pre><code>#load json  
  try:
      with open('tmp.p', 'rb') as f:
          myjson = json.load(f) 
  except IOError:
      print("Error converting to json")
  
  #write selected json to a csv output file      
  out = open(savedpath, 'a+')
      try:
          #add or remove data to parse here    
          mydata = ('d','b','a')
  
          mycsv = csv.DictWriter(out, fieldnames=mydata, quoting=csv.QUOTE_ALL, extrasaction='ignore', lineterminator='\n')
          mycsv.writeheader()
          for row in myjson["data"]:
              mycsv.writerow(row)
      finally:
          out.close()
  </code></pre>
  
  <p>I've parsed the nested elements to a separate temp file:</p>
  
  <pre><code># parse nested elements ##
  try:
          #add or remove Port1/Port2 data to parse here
          myport = ('bb','ba')
          tmp3 = csv.DictWriter(t3, fieldnames=myport, quoting=csv.QUOTE_ALL, extrasaction='ignore', lineterminator='\n')
          tmp3.writeheader()
          tmp4 = csv.DictWriter(t4, fieldnames=myport, quoting=csv.QUOTE_ALL, extrasaction='ignore', lineterminator='\n')
          tmp4.writeheader()        
          #print myjson["data"][0]["b"]["bb"]
          #print myjson["data"][0]["c"]["bb"]
  
          for row in myjson["data"]:
              data1 = row["b"]
              data2 = row["c"]
              #print data1["bb"]
  
              tmp3.writerow(data1)
              tmp4.writerow(data2)
      finally:
          t3.close()
          t4.close()
  </code></pre>
  
  <p>But I am having troubles trying to join the data. I want the data to look like this in my csv:</p>
  
  <pre><code>#header
  a:1
  b:1
  #data
  a,d,ba,bc,ca,cc
  1,4,b1,b3,x1,x3
  12,12,12a,1ab,12z,12zzz
  </code></pre>
  
  <p>I'm stuck when trying to write my csv file. I think I'm over-thinking this, but since I thought using concatenating strings might work, but it didn't:</p>
  
  <pre><code>try:
          with open('tmp3.p', 'rb') as port1:
              with open('tmp4.p', 'rb') as port2:
                  with open('tmp5.p', 'rb') as general:
                      for rport1 in port1:
                          for rport2 in port2:
                              for rgen in general:
                                  rport1 = str.replace(rport1,"\n","")
                                  rport2 = str.replace(rport2,"\n","")                        
                                  rgen = str.replace(rgen,"\n","")                        
                                  string = ("%s,%s,%s" % (rgen, rport1, rport2))
                                  print string
  </code></pre>
  
  <p>I used DictWriter because I need the file to be parsed at a certain order. I know I'm defeating the purpose of using json when trying to combine the data, and it's a really bad practice, but yet I'm not sure how to proceed. Thank you in advance for helping...</p>