-1

I am creating a Python dictionary and the results contain a few thousand entries like this:

{
    "stations": ".",
    "Alt-Tagel": ".",
    "id": 5718454109,
    "name": "Alt-Tegel",
    "label": "Alt-Tegel",
    "position": ".",
    "lat": 52.5894565,
    "lon": 13.2837514
}

My program

import json

# Data to be written

def bus_line8():
    keys = ['stations', 'Alt-Tegel', 'id', 'name', 'label', 'position' ,'lat', 'lon']
    values = ['{', '{', 5718454109, 'Alt-Tegel', 'Alt-Tegel', '{' , 52.5894565, 13.2837514]
    dictionary = dict(zip(keys, values))
    print(dictionary) # {'a': 1, 'b': 2, 'c': 3}

    # Serializing json
    json_object = json.dumps(dictionary, indent = 4)

    # Writing to sample.json
    with open("bus_line.json", "w") as outfile:
        outfile.write(json_object)

But I want this:

{
    "stations": {
        "Alt-Tegel": {
            "id": 5718454109,
            "name": "Alt-Tegel",
            "label": "Alt-Tegel ",
            "position": {
                "lat": 52.5894565,
                "lon": 13.2837514
            }

Can someone help me please I'm on it since 2 days.

martineau
  • 119,623
  • 25
  • 170
  • 301
Tahiry-R
  • 11
  • 1
  • it would help if you could show the actual source data instead of a hard-coded data entry. As you can see we can answer your question, but whether or not it's actually helpful is debatable. With the source data (some example entries from actual real-life data) we can answer your question a lot better. – Edo Akse Sep 19 '21 at 11:14
  • Does this answer your question? [How to prettyprint a JSON file?](https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file) – Ivan Shelonik Sep 24 '21 at 12:29
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 24 '21 at 19:13

1 Answers1

0

Try this out:

import json

def process_result(result):
    return {
        'stations': {
            result['label']: {
                'id': result['id'],
                'name': result['name'],
                'label': result['label'],
                'position': {
                    'lat': result['lat'],
                    'lon': result['lon']
                }
            }
        }
    }


result = {
    "stations": ".",
    "Alt-Tagel": ".",
    "id": 5718454109,
    "name": "Alt-Tegel",
    "label": "Alt-Tegel",
    "position": ".",
    "lat": 52.5894565,
    "lon": 13.2837514
}

result = process_result(result)
result_json = json.dumps(result, indent=4)
print(result_json)

Output:

{
    "stations": {
        "Alt-Tegel": {
            "id": 5718454109,
            "name": "Alt-Tegel",
            "label": "Alt-Tegel",
            "position": {
                "lat": 52.5894565,
                "lon": 13.2837514
            }
        }
    }
}

I hope it's what you had in mind.

Toni Sredanović
  • 2,280
  • 1
  • 11
  • 13
  • 1
    While this code may answer the question, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit](https://meta.stackoverflow.com/posts/360251/edit) your answer to add some explanation, including the assumptions you’ve made. – martineau Sep 17 '21 at 07:37
  • Yes It's like that thank you so much ! :D – Tahiry-R Sep 17 '21 at 07:38
  • @Tahiry-R Could you please accept the answer then? – Toni Sredanović Sep 17 '21 at 08:08