Home > Back-end >  How might I output an array of dictionaries on different lines on a JSON file?
How might I output an array of dictionaries on different lines on a JSON file?

Time:11-07

import json
import csv


def load_matching_data(min_scores,min_stories,min_units):
  array = []
  with open("apartment_building_evaluation.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=",")
    for column in csv_reader:
      address = column[26]
      if (int(float(column[2])) >= min_stories) and (int(float(column[3]))>= min_units) and ((float(column[24]))>= min_scores):
        dict = {"address" : address, "score": float(column[24]), "num_stories": int(float(column[2])), "num_units": int(float(column[3]))}
        array.append(dict)
    return array
result = load_matching_data(82,28,300)

I have gotten this output which is an array of dictionaries which was what I wanted

returned value:  [{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337}, {"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336}, {"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463}, {"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500}, {"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369}, {"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493}, {"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388}, {"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496}, {"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575}, {"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406}, {"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342}, {"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319}, {"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}]

that I want to upload to a JSON file using this function:

def save_summary(list,string):
  with open('apartment_summary.json', 'w',encoding='utf-8') as outfile:
    json.dump(list, outfile)
  return outfile



save_summary(results, 'apartment_summary.json')#appartment_summary.json is the name of the file.

I can already upload it to the JSON file. My question is how might I be able to upload this array to the JSON file where each dictionary in the curly brackes are printed onto different lines like this:

    [
    {"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337},
    {"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336},
    {"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463},
    {"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483},
    {"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500},
    {"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369},
    {"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493},
    {"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388},
    {"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369},
    {"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496},
    {"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575},
    {"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406},
    {"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342},
    {"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319},
    {"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}
    ]

I have tried using the indent function but it produces this output.

[
  {
    "address": "252  VICTORIA ST ",
    "score": 95.0,
    "num_stories": 37,
    "num_units": 337
  },
  {
    "address": "320  TWEEDSMUIR AVE ",
    "score": 93.0,
    "num_stories": 30,
    "num_units": 336
  },
  {
    "address": "570  BAY ST ",
    "score": 100.0,
    "num_stories": 29,
    "num_units": 463
  },
  {
    "address": "77  DAVISVILLE AVE ",
    "score": 90.0,
    "num_stories": 30,
    "num_units": 483
  },
  {
    "address": "85-95  THORNCLIFFE PARK DR ",
    "score": 93.0,
    "num_stories": 43,
    "num_units": 500
  },
  {
    "address": "561  SHERBOURNE ST ",
    "score": 99.0,
    "num_stories": 43,
    "num_units": 369
  },
  {
    "address": "140  ERSKINE AVE ",
    "score": 98.0,
    "num_stories": 29,
    "num_units": 493
  },
  {
    "address": "167  CHURCH ST ",
    "score": 98.0,
    "num_stories": 28,
    "num_units": 388
  },
  {
    "address": "22  JOHN ST ",
    "score": 99.0,
    "num_stories": 31,
    "num_units": 369
  },
  {
    "address": "85-95  THORNCLIFFE PARK DR ",
    "score": 94.0,
    "num_stories": 43,
    "num_units": 496
  },
  {
    "address": "45  DUNFIELD AVE ",
    "score": 91.0,
    "num_stories": 30,
    "num_units": 575
  },
  {
    "address": "6  FOREST LANEWAY  ",
    "score": 91.0,
    "num_stories": 29,
    "num_units": 406
  },
  {
    "address": "118  BALLIOL ST ",
    "score": 86.0,
    "num_stories": 30,
    "num_units": 342
  },
  {
    "address": "500  DUPLEX AVE ",
    "score": 87.0,
    "num_stories": 33,
    "num_units": 319
  },
  {
    "address": "15  ROEHAMPTON AVE ",
    "score": 100.0,
    "num_stories": 36,
    "num_units": 466
  }
]

Can someone please explain to me what I am doing wrong and how I may fix this problem?

CodePudding user response:

You can do something like

import json

result = [{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337}, {"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336}, {"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463}, {"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500}, {"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369}, {"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493}, {"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388}, {"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496}, {"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575}, {"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406}, {"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342}, {"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319}, {"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}]

txt = ',\n'.join(json.dumps(rec) for rec in result)

lines = f'[\n{txt}\n]'

with open('out.json', 'w') as f:
  f.write(lines)

then lines should contain the data formatted in the way you want

out.json after running the above:

[
{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337},
{"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336},
{"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463},
{"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483},
{"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500},
{"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369},
{"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493},
{"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388},
{"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369},
{"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496},
{"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575},
{"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406},
{"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342},
{"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319},
{"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}
]

CodePudding user response:

Here a simple way to do it:

import json

result = [{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337}, {"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336}, {"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463}, {"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500}, {"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369}, {"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493}, {"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388}, {"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496}, {"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575}, {"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406}, {"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342}, {"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319}, {"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}]
results = (['[\n']   [',\n'.join(json.dumps(building) for building in result)]   ['\n]'])
upload_json = ''.join(results)
print(upload_json)

Output:

[
{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337},
{"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336},
{"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463},
{"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483},
{"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500},
{"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369},
{"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493},
{"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388},
{"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369},
{"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496},
{"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575},
{"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406},
{"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342},
{"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319},
{"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}
]

CodePudding user response:

One last way to do it is to use map to call json.dumps on each element in the result list. For example:

import json

result = [{"address": "252  VICTORIA ST ", "score": 95.0, "num_stories": 37, "num_units": 337}, {"address": "320  TWEEDSMUIR AVE ", "score": 93.0, "num_stories": 30, "num_units": 336}, {"address": "570  BAY ST ", "score": 100.0, "num_stories": 29, "num_units": 463}, {"address": "77  DAVISVILLE AVE ", "score": 90.0, "num_stories": 30, "num_units": 483}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 93.0, "num_stories": 43, "num_units": 500}, {"address": "561  SHERBOURNE ST ", "score": 99.0, "num_stories": 43, "num_units": 369}, {"address": "140  ERSKINE AVE ", "score": 98.0, "num_stories": 29, "num_units": 493}, {"address": "167  CHURCH ST ", "score": 98.0, "num_stories": 28, "num_units": 388}, {"address": "22  JOHN ST ", "score": 99.0, "num_stories": 31, "num_units": 369}, {"address": "85-95  THORNCLIFFE PARK DR ", "score": 94.0, "num_stories": 43, "num_units": 496}, {"address": "45  DUNFIELD AVE ", "score": 91.0, "num_stories": 30, "num_units": 575}, {"address": "6  FOREST LANEWAY  ", "score": 91.0, "num_stories": 29, "num_units": 406}, {"address": "118  BALLIOL ST ", "score": 86.0, "num_stories": 30, "num_units": 342}, {"address": "500  DUPLEX AVE ", "score": 87.0, "num_stories": 33, "num_units": 319}, {"address": "15  ROEHAMPTON AVE ", "score": 100.0, "num_stories": 36, "num_units": 466}]

upload_json = '[\n'   ',\n'.join(map(json.dumps, result))   '\n]'
print(upload_json)

Output should be the same as for the other solutions.

  • Related