0

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}
    .....
  ];

11
  • Show us what you have tried so far! Commented Jul 13, 2020 at 20:54
  • @KlausD. Thank you for your comment. I edited the post. Could you check it again? Commented Jul 13, 2020 at 20:58
  • What is the problem with the code you have? Commented Jul 13, 2020 at 21:03
  • You access features immediate, but there's nothing like that in your file. Commented Jul 13, 2020 at 21:05
  • @KlausD. Sorry to make you confused. I posted the original JSON file as well. Commented Jul 13, 2020 at 21:08

3 Answers 3

1

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}
]
Sign up to request clarification or add additional context in comments.

Comments

0

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}]

Comments

0

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},
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.