emie
emie

Reputation: 289

Python: Appending JSON and getting too many Curly Brackets

After running my app, I store the history within ProcessingJob.json file. However, when I attempt to append the JSON file, the code write an additional set of {} brackets around the latest object.

You will see that there is an extra {} bracket around the "05-01-2020" node, before and at the end of the node. I've tried multiple times to correct/fix the code but this is the only way I can get it to run. Have any suggestions on how to improve?

#where write_type == 1 is Dictionary-<date>.json, and write_type == 2 is ProcessingAudit.json
def updateJSON_File(write_type,inputDS,input_jFile, output_jFile):   
with open(input_jFile, 'r') as f: 
    data = json.load(f) 

    i = 1

    if write_type==1:
        for index, row in inputDS.iterrows():
            y = {
                "QueryText": row[0],
                "Subjectmatter": row[1],
                "DateAdded": row[2],
                "SimilarityScore": row[3]                
            }

            data.append(y)

    elif write_type==2:
        while (i<=2): 
            if i==1:
                temp = data['analysis']

                # python object to be appended
                y = {
                    inputDS[1]: {
                        "dictionaryFileName": inputDS[2],
                        "analysisFileName": inputDS[3], 
                        "totalEvaluated": inputDS[4],
                        "netNewLabelledEntries": inputDS[9],
                       "range95": inputDS[5],
                        "range85": inputDS[6],
                        "range75": inputDS[7],
                        "rangeLessThan75": inputDS[8] 
                     }
                }

                # appending data to emp_details  
                temp.append(y)

                i += 1

            elif i==2:
                tmp = data["lastAnalysisCompleted"]
                data["lastAnalysisCompleted"] = inputDS[1]

                i+=1
            else:
                i==3
                print("error")
    else:
        pass

    #update Processing Data JSON file
    with open(output_jFile,'w') as f: 
        json.dump(data, f, indent=1) 

    if write_type == 1:
        return 1
    else:
        return inputDS[3]

Here is the ProcessingJob.json file.

{
 "lastAnalysisCompleted": "05-01-2020",
 "analysis": [
  {
   "12-31-2019": {
    "dictionaryFileName": "Dictionary/Dictionary_12-31-2019.json",
    "analysisFileName": "Analysis/Analysis_Output_12-31-2019.xlsx",
    "totalEvalauated": 100,
    "netNewAdded": 10,
    "range95": 1,
    "range80": 3,
    "range75": 1,
    "rangeLessThan75": 5
   },
   "01-01-2020": {
    "dictionaryFileName": "Dictionary/Dictionary_01-01-2020.json",
    "analysisFileName": "Analysis/Analysis_Output_01-01-2020.xlsx",
    "totalEvalauated": 100,
    "netNewAdded": 10,
    "range95": 1,
    "range80": 3,
    "range75": 1,
    "rangeLessThan75": 5
   },
   "04-30-2020": {
    "dictionaryFileName": "Dictionary/Dictionary_04-30-2020.json",
    "analysisFileName": "Analysis/Analysis_Output_04-30-2020.xlsx",
    "totalEvaluated": 289,
    "netNewLabelledEntries": 8,
    "range95": 8,
    "range85": 57,
    "range75": 85,
    "rangeLessThan75": 139
   }
  },
  {
   "05-01-2020": {
    "dictionaryFileName": "Dictionary/Dictionary_05-01-2020.json",
    "analysisFileName": "Analysis/Analysis_Output_05-01-2020.xlsx",
    "totalEvaluated": 297,
    "netNewLabelledEntries": 16,
    "range95": 16,
    "range85": 57,
    "range75": 85,
    "rangeLessThan75": 149
   }
  }
 ]
}

Upvotes: 0

Views: 1167

Answers (2)

Onno Rouast
Onno Rouast

Reputation: 672

in your JSON data analysis is an array [] containing an anonymous object {}. The array corresponds to a python list, and the object to a dictionary

If you want to add a new date to this anonymous object, you can select it positionally as the first item in the array [0], and add your new item to this via your dictionary key.

data["analysis"][0][inputDS[1]] = y

with y being your data for the key inputDS[1]


y = {
    "dictionaryFileName": inputDS[2],
    "analysisFileName": inputDS[3],
    "totalEvaluated": inputDS[4],
    "netNewLabelledEntries": inputDS[9],
    "range95": inputDS[5],
    "range85": inputDS[6],
    "range75": inputDS[7],
    "rangeLessThan75": inputDS[8]
}

Upvotes: 1

deadshot
deadshot

Reputation: 9061

You just need update the dictionary inside the list with new result.

temp[0].update(y)

Upvotes: 0

Related Questions