-1

Actually i am pushing data to other system but before pushing i have to change the "key" in the whole JSON. JSON may contain 200 or 10000 or 250000 data.

sample JSON:

{
  "insert": "table",
  "contacts": [
    {
      "testName": "testname",
      "ContactID": 212121
    },
    {
      "testName": "testname",
      "ContactID": 2146354564
       },
    {
      "testName": "testname",
      "ContactID": 12312
        },
    {
      "testName": "testname",
      "ContactID": 211221

    },
    {
      "testName": "testname",
      "ContactID": 10218550

    }
  ]
}

I need to change contacts array Keys. These contacts may be in bulk. So i need to work with this efficiently with minimal complexity.

The above JSON to be converted as below

{
  "insert": "table",
  "contacts": [
    {
      "name": "testname",
      "phone": 212121
    },
    {
      "name": "testname",
      "phone": 2146354564
       },
    {
      "name": "testname",
      "phone": 12312
        },
    {
      "name": "testname",
      "phone": 211221

    },
    {
      "name": "testname",
      "phone": 10218550

    }
  ]
}

here is my code trying by loop

ini_dict = request.data
contact_data = ini_dict['contacts']
for i in contact_data: 
    i['name'] = i.pop('testName')
print(contact_data)

Please suggest me how can i change the key names efficiently for bulk data. i mean for 50000 lists in contacts. "for loop" will be leading a performance issue. So please let me know the efficient way to achieve this

user6250770
  • 680
  • 2
  • 10
  • 25

1 Answers1

0

I dont know how fast you need it to be nor how you are choosing to store your json. One simple solution is just store it as a string and then replace all the instances of your attributes.

# Something like this using a jsonstring

jsonstring.replace("'testName':", "'name':")
jsonstring.replace("'ContactId':", "'phone':")

If you want to do this in bulk you, may need to create some batch process to be able to fetch multiple existing records and make changes at once. I have done this before with the java equivalent of https://pypi.org/project/JayDeBeApi/ but, that was more for modifying existing records in a database.

duppydodah
  • 165
  • 1
  • 3
  • 17