skip to Main Content

I need to change values of items "Id" "SummaryId" and "CoworkersId" in my json. I have already code which will change values of "Id" and "SummaryId". Code below generates new unique Id for these 2 items. I need that this code will change "CoworkersId" as well. As you can see the same value is in these 3 items – a4555752s. The issue is that item "CoworkersId" is a list, not a string and contain 2 elements.

import json
import random

class Cache:
    # A dictionary that keeps a record of "seen" keys
    key_cache = {}

    @classmethod
    def generate_replacement_key(cls, key: str) -> str:
        """
            If key has already been seen, then return the cached key for that, 
            otherwise generate and store a new key.
        """
        new_key = cls.key_cache.setdefault(key, cls.generate_random_key())
        return new_key
    
    @classmethod
    def generate_random_key(cls) -> str:
        """
            returns a random key, that hasn't been generated before
        """
        new_key = str(random.randint(1000, 1000000))
        if new_key in cls.key_cache.values():
            return cls.generate_random_key()
        return new_key
        
a = '''
{
    "people": [
        {
            "Name": "Jan",
            "Lastname": "Szewc",
            "Id": "a4555752s"
        },
        {
            "Name": "Marek",
            "Lastname": "Piorun",
            "Id": "a4asa85",
            "SummaryId": "a4555752s"
        },
        {
            "Name": "Mikolaj",
            "Lastname": "Ciekasz",
            "Id": "a4ddd244",
            "ManagerId": "a4555752s",
            "CoworkersId":
            [   "1278978",
                "a4555752s"]
        }
]}
'''
data = json.loads(a)


for people in data["people"]:
    # use walrus operator to make less boilerplate-code.
    if (id_string := people.get("Id")): # use dict.get to avoid key-errors.
        people["Id"] = Cache.generate_replacement_key(id_string)
    if (summary_id_string := people.get("SummaryId")):
        people["SummaryId"] = Cache.generate_replacement_key(summary_id_string)
print(json.dumps(data, indent=2))

I need that values of all 3 elements "Id", "SummaryId" and "CoworkersId" will be changed.

2

Answers


  1. I think I got it, but there must be more efficient method, this is just a quick thing.
    SummeryId and Id of the first object are the same because in json that you supplied they are the same, this means that:

    key_cache = {}
    
    @classmethod
        def generate_replacement_key(cls, key: str) -> str:
            new_key = cls.key_cache.setdefault(key, cls.generate_random_key())
            return new_key
    

    will return the same value since it already exists in the dict.

    
    data = (json.loads(a))['people']
    alist = [] 
    for people in data:
        newId = ''
        if (id_string := people.get("Id")): # use dict.get to avoid key-errors.
            newId = Cache.generate_replacement_key(id_string)
            people["Id"] = newId
            alist.append(newId)
            
        if (summary_id_string := people.get("SummaryId")):
            newId = Cache.generate_replacement_key(summary_id_string)
            people["SummaryId"] = newId
        
    for obj in data:
        if "CoworkersId" in obj:     
            for idx, i in enumerate(alist):
                if i == obj.get('Id'):
                    alist.pop(idx)
            obj["CoworkersId"] = alist
            
    print(json.dumps(data, indent=2))
    

    Output

    [
      {
        "Name": "Jan",
        "Lastname": "Szewc",
        "Id": "195229"
      },
      {
        "Name": "Marek",
        "Lastname": "Piorun",
        "Id": "202461",
        "SummaryId": "195229"
      },
      {
        "Name": "Mikolaj",
        "Lastname": "Ciekasz",
        "Id": "114871",
        "ManagerId": "a4555752s",
        "CoworkersId": [
          "195229",
          "202461"
        ]
      }
    ]
    
    Login or Signup to reply.
  2. You can loop through all the coworker ids to modify them, like this:

    for people in data["people"]:
        # use walrus operator to make less boilerplate-code.
        if (id_string := people.get("Id")): # use dict.get to avoid key-errors.
            people["Id"] = Cache.generate_replacement_key(id_string)
        if (summary_id_string := people.get("SummaryId")):
            people["SummaryId"] = Cache.generate_replacement_key(summary_id_string)
        
        # Here's the new code, looping through all coworker ids and changing them
        if (coworker_id_list := people.get("CoworkersId")):
            for i, coworker_id_string in enumerate(coworker_id_list):
                coworker_id_list[i] = Cache.generate_replacement_key(coworker_id_string)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search