Home > Blockchain >  Modify data from a json in python
Modify data from a json in python

Time:12-23

I´m in a situation where a client has sent me a .json file, this file contents different shapes in it:

{
  "version": "5.0.1",
  "flags": {},
  "shapes": [
    {
      "label": "weed",
      "points": [
        [
          1131.3513513513515,
          1718.918918918919
        ],
        [
          1225.945945945946,
          1681.081081081081
        ],
        [
          1239.4594594594594,
          1716.2162162162163
        ],
        [
          1274.5945945945946,
          1740.5405405405406
        ],
        [
          1312.4324324324325,
          1802.7027027027027
        ],
        [
          1288.1081081081081,
          1854.0540540540542
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "weed",
      "points": [
        [
          1866.4864864864867,
          1837.837837837838
        ],        
        [
          1728.6486486486488,
          1908.1081081081081
        ],
        [
          1701.6216216216217,
          1867.5675675675675
        ],
        [
          1728.6486486486488,
          1864.8648648648648
        ],
        [
          1769.1891891891892,
          1867.5675675675675
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "weed",
      "points": [
        [
          2015.135135135135,
          967.5675675675676
        ],
        [
          1974.5945945945946,
          940.5405405405405
        ],
        [
          1974.5945945945946,
          886.4864864864865
        ],
        [
          1996.2162162162163,
          856.7567567567568
        ],
        [
          2031.3513513513512,
          818.918918918919
        ],
        [
          2109.7297297297296,
          794.5945945945946
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },

As you can see i have different polygones in the same file. Using an algorithm I created i can create three new points from those points i get. The new points i obtain are the ones I need to write where the points I receive are. I can write them in the json file if i only get one polygon, but when i get several, like in the example i pasted when i write the points of the first two polygons are overwritten by the last ones. My code:

with open('data.json') as file:
    data = json.load(file)

    for shape in data['shapes']:
        # print("Shape type: ", shape['shape_type'])
        if shape['shape_type'] == 'polygon':
            # == 'polygon or != 'rectangle and != 'square
            for points in data['shapes']:
                # read the points
                # algorithm
                 new_points = [x1, x2, x3]

            # Write the new points and change the sahpe_type to rectangle in the json
            new_points = [bottom_right, bottom_left, top_right, top_left]
            shape['points'] = new_points
            shape['shape_type'] = "rectangle"
        else:
            pass
   with open('data.json', 'w') as f:
       json.dump(data, f)

I tryed moving the json.dump:

with open('data.json') as file:
    data = json.load(file)

    for shape in data['shapes']:
        # print("Shape type: ", shape['shape_type'])
        if shape['shape_type'] == 'polygon':
            # == 'polygon or != 'rectangle and != 'square
            for points in data['shapes']:
                # read the points
                # algorithm
                 points_np = np.array(points['points'])
                 points_np_t = points_np.transpose()
                 x1 = np.min.(points_np_t[0])
                 x2 = np.max.(points_np_t[0])
                 x3 = np.min.(points_np_t[1])
                 new_points = [x1, x2, x3]

            # Write the new points and change the sahpe_type to rectangle in the json
            new_points = [bottom_right, bottom_left, top_right, top_left]
            shape['points'] = new_points
            shape['shape_type'] = "rectangle"

            with open('data.json', 'w') as f:
                json.dump(data, f)

        else:
            pass

CodePudding user response:

You can try the following:

import json 

# loading json data 
data = json.load(open('data.json')) 

# modifying data 
data['key'] = 'value'

# writing to json file 
with open('data.json', 'w') as f: 
    json.dump(data, f)

CodePudding user response:

You almost had it, a few misused variables, scope and for-loops.

import json
import numpy as np

def run():
    with open('data.json') as file:
        data = json.load(file)

        for shape in data['shapes']:
            if shape['shape_type'] == 'polygon':
                # at this point, you have access to the all the 
                # points by `shape['points']`
                # manipulate them as one pleases
                points_np = np.array(shape['points'])
                points_np_t = points_np.transpose()
                x1 = np.min(points_np_t[0])
                x2 = np.max(points_np_t[0])
                x3 = np.min(points_np_t[1])
                
                # override old points with the new points...
                shape['points'] = [x1, x2, x3]
                shape['shape_type'] = "rectangle"
            else:
                pass
    
        with open('new_file.json', 'w') as f:
            json.dump(data, f)

if __name__ == "__main__":
    run()

I also noticed that the JSON you posted was not valid

{
    "version": "5.0.1",
    "flags": {},
    "shapes": [
      {
        "label": "weed",
        "points": [
          [
            1131.3513513513515,
            1718.918918918919
          ],
          [
            1225.945945945946,
            1681.081081081081
          ],
          [
            1239.4594594594594,
            1716.2162162162163
          ],
          [
            1274.5945945945946,
            1740.5405405405406
          ],
          [
            1312.4324324324325,
            1802.7027027027027
          ],
          [
            1288.1081081081081,
            1854.0540540540542
          ]
        ],
        "group_id": null,
        "shape_type": "polygon",
        "flags": {}
      },
      {
        "label": "weed",
        "points": [
          [
            1866.4864864864867,
            1837.837837837838
          ],        
          [
            1728.6486486486488,
            1908.1081081081081
          ],
          [
            1701.6216216216217,
            1867.5675675675675
          ],
          [
            1728.6486486486488,
            1864.8648648648648
          ],
          [
            1769.1891891891892,
            1867.5675675675675
          ]
        ],
        "group_id": null,
        "shape_type": "polygon",
        "flags": {}
      },
      {
        "label": "weed",
        "points": [
          [
            2015.135135135135,
            967.5675675675676
          ],
          [
            1974.5945945945946,
            940.5405405405405
          ],
          [
            1974.5945945945946,
            886.4864864864865
          ],
          [
            1996.2162162162163,
            856.7567567567568
          ],
          [
            2031.3513513513512,
            818.918918918919
          ],
          [
            2109.7297297297296,
            794.5945945945946
          ]
        ],
        "group_id": null,
        "shape_type": "polygon",
        "flags": {}
      }
    ]
}
  • Related