CalgaryFlames
CalgaryFlames

Reputation: 706

Parsing JSON file with multiple arrays using Python

I tried to retrieve the data from JSON file and assign them into object type like code the below. The data has multiple arrays in order with lat and lng. Using Python how could I deal with this one?

Current Python code*

import os
from flask import Flask, render_template, abort, url_for, json
import json

app = Flask(__name__)

...
with open('./data/file.json', 'r') as myfile:
    data = myfile.read()
    features = json.loads(data)['features']
    for o in features:
        print(o['coordinates'])). <----'Data from JSON file'
...
app.run(host='localhost', debug=True)

Orginal JSON file

{
    "features": [{
        "coordinates": [
            [
                [
                    -79.3998992207101,
                    43.770625433748776
                ],
                [
                    -79.39977945240246,
                    43.770651091617324
                ],
                [
                    -79.39970177723474,
                    43.77046066096583
                ],
                [
                    -79.39982154480901,
                    43.77043500133246
                ],
                [
                    -79.3998992207101,
                    43.770625433748776
                ]
            ]
        ]
    }]
}

Data from JSON file

[[[-79.3998992207101, 43.770625433748776], [-79.39977945240246, 43.770651091617324], [-79.39970177723474, 43.77046066096583], [-79.39982154480901, 43.77043500133246], [-79.3998992207101, 43.770625433748776]]]

Result I am aiming at

var triangleCoords = [
    {lat: -79.3998992207101, lng: 43.770625433748776},
    {lat: -79.39977945240246, lng: 43.770651091617324},
    {lat: -79.39970177723474, lng: 43.77042949785241},
    {lat: -79.39987169202237, lng: 43.77039053223808}
    .....
  ];

Upvotes: 0

Views: 1313

Answers (3)

Sherzod Sadriddinov
Sherzod Sadriddinov

Reputation: 116

You can use this really easy function, just replace print(o['coordinates'])) with function name as I show in example

def unpack_coordinates(coordinates):
    coordinates_list = list()
    for item in coordinates[0]:
        coordinates_list.append({"lat": item[0], "lng": item[1]})
    return coordinates_list

# Your Flask code
...
with open('JSON.json', 'r') as myfile:
    data = myfile.read()
    features = json.loads(data)["features"]
    for o in features:
        print(unpack_coordinates(o["coordinates"]))
...

Output

[
    {'lat': -79.3998992207101, 'lng': 43.770625433748776}, 
    {'lat': -79.39977945240246, 'lng': 43.770651091617324}, 
    {'lat': -79.39970177723474, 'lng': 43.77046066096583}, 
    {'lat': -79.39982154480901, 'lng': 43.77043500133246}, 
    {'lat': -79.3998992207101, 'lng': 43.770625433748776}
]

Upvotes: 1

Vishal Singh
Vishal Singh

Reputation: 6234

depending upon how you truncated you JSON there is a possibility of making it less nested.

import json

with open('./data/file.json', 'r') as myfile:
    data = myfile.read()
    features = json.loads(data)["features"]
    triangleCoords = []
    for o in features:
        for key, values in o.items():
            for value in values:
                for v in value:
                    triangleCoords.append({"lat": v[0], "lng": v[1]})

print(triangleCoords)

Output:

[
    {"lat": -79.3998992207101, "lng": 43.770625433748776},
    {"lat": -79.39977945240246, "lng": 43.770651091617324},
    {"lat": -79.39970177723474, "lng": 43.77046066096583},
    {"lat": -79.39982154480901, "lng": 43.77043500133246},
    {"lat": -79.3998992207101, "lng": 43.770625433748776},
]

Upvotes: 0

bigbounty
bigbounty

Reputation: 17368

Keep only original json file in context here

In [87]: a = json.laod(open("./data/file.json"))
In [88]: triangleCoords = []

In [89]:  for i in a["features"]:
    ...:     for j in i["coordinates"][0]:
    ...:        triangleCoords.append({"lat":j[0],"lng":j[1]})
    ...:

In [90]: triangleCoords
Out[90]:
[{'lat': -79.3998992207101, 'lng': 43.770625433748776},
 {'lat': -79.39977945240246, 'lng': 43.770651091617324},
 {'lat': -79.39970177723474, 'lng': 43.77046066096583},
 {'lat': -79.39982154480901, 'lng': 43.77043500133246},
 {'lat': -79.3998992207101, 'lng': 43.770625433748776}]

Upvotes: 0

Related Questions