Python Forum
difficulties to chage json data structure using json module in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difficulties to chage json data structure using json module in python
#1
Good Day, Python community

I am trying to create a script to change THIS JSON data structure:
{ "16bec99d-a13e-4a21-a761-f673631b6060": { "ImageName": "chp/chp08.png", "Fill": 844168280, "Line": -11469736, "ID": "16bec99d-a13e-4a21-a761-f673631b6060", "ParentID": "94021e84-2a1e-440d-9f37-b349fa2cd0d8", "Name": "198008", "IsPolygon": true, "R": 0, "Lat": [ 58.496235, 58.496299, 58.49533, 58.495213 ], "Lng": [ 103.794173, 103.797618, 103.797715, 103.796126 ], "Holes": null }, "8dc91f95-666e-4137-b29b-b12dbbddbdbe": { "ImageName": "chp/chp08.png", "Fill": 844168280, "Line": -11469736, "ID": "8dc91f95-666e-4137-b29b-b12dbbddbdbe", "ParentID": "94021e84-2a1e-440d-9f37-b349fa2cd0d8", "Name": "198005", "IsPolygon": true, "R": 0, "Lat": [ 58.484494, 58.484392, 58.483932, 58.484163 ], "Lng": [ 103.625914, 103.626353, 103.625889, 103.625286 ], "Holes": null } }
TO THIS JSON STRUCTURE:

{ "features": [ { "geometry": { "rings": [ [ [103.794173,58.496235], [103.797618,58.496299], [103.797715,58.49533], [103.796126,58.495213] ] ], }, "attributes": { "Name": "123456", "ID": "16bec99d-a13e-4a21-a761-f673631b6060", "Fill": 844168280, "Line": -11469736 } }, { "geometry": { "rings": [ [ [103.625914,58.484494], [103.626353,58.484392], [103.625889,58.483932], [103.625286,58.484163] ] ], }, "attributes": { "Name": "689651", "ID": "8dc91f95-666e-4137-b29b-b12dbbddbdbe", "Fill": 844168280, "Line": -11469736 } } ] }
I am newbie in Python (and in programming in general), so I spent 3 days literally for nothing
and would like to ask your help. At least, maybe you can point me in the right direction.

Here is my attempt:
import json data = {'16bec99d-a13e-4a21-a761-f673631b6060': {'ImageName': 'chp/chp08.png', 'Fill': 844168280, 'Line': -11469736, 'ID': '16bec99d-a13e-4a21-a761-f673631b6060', 'ParentID': '94021e84-2a1e-440d-9f37-b349fa2cd0d8', 'Name': '198008', 'IsPolygon': True, 'R': 0, 'Lat': [58.496235, 58.496299, 58.49533, 58.495213], 'Lng': [103.794173, 103.797618, 103.797715, 103.796126], 'Holes': None}, '8dc91f95-666e-4137-b29b-b12dbbddbdbe': {'ImageName': 'chp/chp08.png', 'Fill': 844168280, 'Line': -11469736, 'ID': '8dc91f95-666e-4137-b29b-b12dbbddbdbe', 'ParentID': '94021e84-2a1e-440d-9f37-b349fa2cd0d8', 'Name': '198005', 'IsPolygon': True, 'R': 0, 'Lat': [58.484494, 58.484392, 58.483932, 58.484163], 'Lng': [103.625914, 103.626353, 103.625889, 103.625286], 'Holes': None}} for d in data: for x,y in zip(data[d]['Lat'],data[d]['Lng']): arcgisJSON = { "features": [ { "geometry": { "rings": [[ x,y ]] }, "attributes": {data[d]['Name'],data[d]['ID'],data[d]['Fill'],data[d]['Line']} } ] } print(arcgisJSON)
The OUTPUT is:
Output:
{'features': [ {'geometry': {'rings': [[58.484163, 103.625286]]}, 'attributes': { -11469736, '198005', 844168280, '8dc91f95-666e-4137-b29b-b12dbbddbdbe'}}]}
My loop gets just one object from data, creates just one array in 'rings' and I can not fetch needed key-value pairs in 'attributes' (just values).

Please, give me at least good advice.

Best wishes,
Sibdar
Reply
#2
I don't see where the names in the final result are coming from, and your target json had some extraneous commas that make it invalid. That said, I tinkered with it and got something working.

The biggest problem is that you're returning in a loop, when logically you need the whole thing. Here's what I ended up with:
def transform(start): return { "features": [ { "geometry": { "rings": [ list(zip(obj["Lng"], obj["Lat"])) ], }, "attributes": { "Name": "123456", "ID": name, "Fill": obj["Fill"], "Line": obj["Line"] } } for name, obj in start.items() ] }
I realize this may be more advanced than you're used to. If you have trouble understanding it, we could re-write it without the list comprehension.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Graph Interpolate Difficulties Tuurbo46 3 881 Nov-19-2025, 08:34 PM
Last Post: deanhystad
  Errors using json file RonR 5 901 Oct-28-2025, 03:00 PM
Last Post: buran
  Convert JSON to CSV Linuxdesire 6 7,801 Oct-15-2025, 12:05 AM
Last Post: Pedroski55
  How to optimize the speed of processing large JSON files in Python without using too sophia2005 3 896 Aug-02-2025, 03:25 PM
Last Post: snippsat
  Convert Json to table formathttps://python-forum.io/thread-38313.html python_student 3 18,504 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Still difficulties using oop paul18fr 3 1,753 Nov-10-2024, 06:12 PM
Last Post: snippsat
  Trying to get JSON object in python and process it further Creepy 2 1,547 Oct-24-2024, 08:46 AM
Last Post: buran
  Write json data to csv Olive 6 2,337 Oct-22-2024, 06:59 AM
Last Post: Olive
  Python directory structure in wqin 11 barryjo 3 1,490 Sep-11-2024, 03:26 PM
Last Post: deanhystad
  get JSON string from URL with Windows credentials shwfgd 0 1,071 Aug-27-2024, 10:08 PM
Last Post: shwfgd

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.