title: Generating a dynamic nested JSON object and array - python
body: <p>As the question explains the problem, I've been trying to generate nested JSON object. In this case I have <code>for</code> loops getting the data out of dictionary <code>dic</code>. Below is the code:</p>
  
  <pre><code>f = open("test_json.txt", 'w')
  flag = False
  temp = ""
  start = "{\n\t\"filename\"" + " : \"" +initial_filename+"\",\n\t\"data\"" +" : " +" [\n"
  end = "\n\t]" +"\n}"
  f.write(start)
  for i, (key,value) in enumerate(dic.iteritems()):
      f.write("{\n\t\"keyword\":"+"\""+str(key)+"\""+",\n")
      f.write("\"term_freq\":"+str(len(value))+",\n")
      f.write("\"lists\":[\n\t")
      for item in value:
          f.write("{\n")
          f.write("\t\t\"occurance\" :"+str(item)+"\n")
          #Check last object
          if value.index(item)+1 == len(value):
              f.write("}\n" 
              f.write("]\n")
          else:
              f.write("},") # close occurrence object
      # Check last item in dic
      if i == len(dic)-1:
          flag = True
      if(flag):
          f.write("}")
      else:
          f.write("},") #close lists object
          flag = False 
  
  #check for flag
  f.write("]") #close lists array 
  f.write("}")
  </code></pre>
  
  <p>Expected output is:</p>
  
  <pre><code>{
  "filename": "abc.pdf",
  "data": [{
      "keyword": "irritation",
      "term_freq": 5,
      "lists": [{
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 2
      }]
  }, {
      "keyword": "bomber",
      "lists": [{
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 2
      }],
      "term_freq": 5
  }]
  }
  </code></pre>
  
  <p>But currently I'm getting an output like below:</p>
  
  <pre><code>{
  "filename": "abc.pdf",
  "data": [{
      "keyword": "irritation",
      "term_freq": 5,
      "lists": [{
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 2
      },]                // Here lies the problem "," before array(last element)
  }, {
      "keyword": "bomber",
      "lists": [{
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 1
      }, {
          "occurance": 2
      },],                  // Here lies the problem "," before array(last element)
      "term_freq": 5
  }]
  }
  </code></pre>
  
  <p>Please help, I've trying to solve it, but failed. Please don't mark it duplicate since I have already checked other answers and didn't help at all. </p>
  
  <p><strong>Edit 1:</strong>
  Input is basically taken from a dictionary <code>dic</code> whose mapping type is <code>&lt;String, List&gt;</code>
  for example: "irritation" => [1,3,5,7,8]
  where irritation is the key,  and mapped to a list of page numbers.
  This is basically read in the outer for loop where key is the keyword and value is a list of pages of occurrence of that keyword.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <pre><code>dic = collections.defaultdict(list) # declaring the variable dictionary
  dic[key].append(value) # inserting the values - useless to tell here
  for key in dic:
      # Here dic[x] represents list - each value of x
      print key,":",dic[x],"\n" #prints the data in dictionary
  </code></pre>