Mike
Mike

Reputation: 161

Create a nested JSON file from a list of lines that contains the key and value mappings

Complicated title, let me explain :)

I have an array full of cyclical data inside of it, in this form:

id: 0 --------------------------------------------------------------------------------
Roma
Via Francesco Saverio Altamura
Via: 0
https://www.immobiliare.it/annunci/p-147276/
tipologia: Appartamento
m2: 113 m²
piano: 1°, con ascensore
p_edificio: 4 piani
prezzo: € 158.000 - € 270.000
balcone: 0
cantina: 0
terrazza: 0
giardino privato: 0
taverna: 0
esposizione esterna: 0
id: 1 --------------------------------------------------------------------------------
Roma
Monti
Via degli Zingari
https://www.immobiliare.it/annunci/89886713/
tipologia: Appartamento
m2: 113 m²
piano: 1°, con ascensore
p_edificio: 4 piani
prezzo: € 790.000
balcone: 0
cantina: 0
terrazza: 0
giardino privato: 0
taverna: 0
esposizione esterna: 0

I added a line to make it visible on where one ends easily. Note that it will not be part of the actual input lines.

What I want to do is to create a JSON file, but not by simply dumping this array inside of the file, I want to create a good structure like the following example:

{
"id": "123"
    "city": "Roma",
    "zone": "Via Francesco Saverio Altamura",
    "via": "0",
    "link": "https://www.immobiliare.it/annunci/p-147276/",
    "prezzo": "€ 158.000 - € 270.000"
    "addons": [
        {
            "balcone": "0",
            "terrazza": "0",
             and so on...
        },
    ]
}

Until now I didn't need to organize it, but now after achieving a good array, with cyclical data that spits out 16 tuple with "key": "value" for every id, I need to organize it and can't understand how to do it how I need it.

Any help on both learning and understanding this process are welcome!

Upvotes: 1

Views: 1017

Answers (2)

Niel Godfrey P. Ponciano
Niel Godfrey P. Ponciano

Reputation: 10709

Assuming the input data has a fixed order on the fields, this is a solution using regex.

import re

data = """
id: 0
Roma
Via Francesco Saverio Altamura
Via: 0
https://www.immobiliare.it/annunci/p-147276/
tipologia: Appartamento
m2: 113 m²
piano: 1°, con ascensore
p_edificio: 4 piani
prezzo: € 158.000 - € 270.000
balcone: 0
cantina: 0
terrazza: 0
giardino privato: 0
taverna: 0
esposizione esterna: 0
id: 1
Roma
Monti
Via degli Zingari
https://www.immobiliare.it/annunci/89886713/
tipologia: Appartamento
m2: 113 m²
piano: 1°, con ascensore
p_edificio: 4 piani
prezzo: € 790.000
balcone: 0
cantina: 0
terrazza: 0
giardino privato: 0
taverna: 0
esposizione esterna: 0
"""

data_fixed_keys = [
    "id",
    "city",
    "zone",
    "via",
    "link",
    "tipologia",
    "m2",
    "piano",
    "p_edificio",
    "prezzo",
    "balcone",
    "cantina",
    "terrazza",
    "giardino privato",
    "taverna",
    "esposizione esterna",
]

matches = re.findall(
    r"id: (?P<id>.*)\s+"
    "(?P<city>.*)\s+"
    "(?P<zone>.*)\s+"
    "(?P<via>.*)\s+"
    "(?P<link>.*)\s+"
    "tipologia: (?P<tipologia>.*)\s+"
    "m2: (?P<m2>.*)\s+"
    "piano: (?P<piano>.*)\s+"
    "p_edificio: (?P<p_edificio>.*)\s+"
    "prezzo: (?P<prezzo>.*)\s+"
    "balcone: (?P<balcone>.*)\s+"
    "cantina: (?P<cantina>.*)\s+"
    "terrazza: (?P<terrazza>.*)\s+"
    "giardino privato: (?P<giardino_privato>.*)\s+"
    "taverna: (?P<taverna>.*)\s+"
    "esposizione esterna: (?P<esposizione_esterna>.*)",
    data,
)

""" Option 1: Dictionary JSON """
data_json = {}
for match in matches:
    current_dict = dict(zip(data_fixed_keys[1:5], match[1:5]))
    current_dict["addons"] = dict(zip(data_fixed_keys[5:], match[5:]))
    current_dict["prezzo"] = current_dict["addons"].pop("prezzo")
    data_json[match[0]] = current_dict

""" Option 2: List JSON
data_json = []
for match in matches:
    current_dict = dict(zip(data_fixed_keys[:5], match[:5]))
    current_dict["addons"] = dict(zip(data_fixed_keys[5:], match[5:]))
    current_dict["prezzo"] = current_dict["addons"].pop("prezzo")
    data_json.append(current_dict)
"""

print(data_json)

Output (pretty printed)

# If using Option 1 (Dictionary JSON)
{
    "0": {
        "city": "Roma",
        "zone": "Via Francesco Saverio Altamura",
        "via": "Via: 0",
        "link": "https://www.immobiliare.it/annunci/p-147276/",
        "addons": {
            "tipologia": "Appartamento",
            "m2": "113 m\u00b2",
            "piano": "1\u00b0, con ascensore",
            "p_edificio": "4 piani",
            "balcone": "0",
            "cantina": "0",
            "terrazza": "0",
            "giardino privato": "0",
            "taverna": "0",
            "esposizione esterna": "0"
        },
        "prezzo": "\u20ac 158.000 - \u20ac 270.000"
    },
    "1": {
        "city": "Roma",
        "zone": "Monti",
        "via": "Via degli Zingari",
        "link": "https://www.immobiliare.it/annunci/89886713/",
        "addons": {
            "tipologia": "Appartamento",
            "m2": "113 m\u00b2",
            "piano": "1\u00b0, con ascensore",
            "p_edificio": "4 piani",
            "balcone": "0",
            "cantina": "0",
            "terrazza": "0",
            "giardino privato": "0",
            "taverna": "0",
            "esposizione esterna": "0"
        },
        "prezzo": "\u20ac 790.000"
    }
}

# If using Option 2 (List JSON)
[
    {
        "id": "0",
        "city": "Roma",
        "zone": "Via Francesco Saverio Altamura",
        "via": "Via: 0",
        "link": "https://www.immobiliare.it/annunci/p-147276/",
        "addons": {
            "tipologia": "Appartamento",
            "m2": "113 m\u00b2",
            "piano": "1\u00b0, con ascensore",
            "p_edificio": "4 piani",
            "balcone": "0",
            "cantina": "0",
            "terrazza": "0",
            "giardino privato": "0",
            "taverna": "0",
            "esposizione esterna": "0"
        },
        "prezzo": "\u20ac 158.000 - \u20ac 270.000"
    },
    {
        "id": "1",
        "city": "Roma",
        "zone": "Monti",
        "via": "Via degli Zingari",
        "link": "https://www.immobiliare.it/annunci/89886713/",
        "addons": {
            "tipologia": "Appartamento",
            "m2": "113 m\u00b2",
            "piano": "1\u00b0, con ascensore",
            "p_edificio": "4 piani",
            "balcone": "0",
            "cantina": "0",
            "terrazza": "0",
            "giardino privato": "0",
            "taverna": "0",
            "esposizione esterna": "0"
        },
        "prezzo": "\u20ac 790.000"
    }
]

Upvotes: 1

matszwecja
matszwecja

Reputation: 8076

json.dumps from built-in json module handles that really nicely, you just need to convert your object to dictionary representing that structure first.

Upvotes: 1

Related Questions