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


Messages In This Thread
difficulties to chage json data structure using json module in python - by Sibdar - Apr-02-2020, 11:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Project Structure for Modularity and Reusability with Multiple Entry Points b19wh33l5 0 191 Apr-24-2024, 12:21 PM
Last Post: b19wh33l5
  encrypt data in json file help jacksfrustration 1 268 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Exclamation Json API JayPy 4 485 Mar-04-2024, 04:28 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 753 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Parsing large JSON josvink66 5 707 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 791 Nov-14-2023, 11:34 PM
Last Post: snippsat
  format json outputs ! evilcode1 3 1,774 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  JSON Dump and JSON Load foxholenoob 8 1,165 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  TypeRoor reading json GreenLynx 3 906 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,590 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020