Question

I use JSON for one of my project. For example, I have the JSON structure.

{
    "address":{
        "streetAddress": {
                  "aptnumber" : "21",
                  "building_number" : "2nd", 
                  "street" : "Wall Street",
            },
        "city":"New York"
    },
    "phoneNumber":
    [
        {
            "type":"home",
            "number":"212 555-1234"
        }
    ]
}

Now I have a bunch of modules using this structure, and it expects to see certain fields in the received json. For the example above, I have two files: address_manager and phone_number_manager. Each will be passed the relevant information. So address_manager will expect a dict that has keys 'streetAddress' and 'city'.

My question is: Is it possible to set up a constant structure so that every time I change the name of a field in my JSON structure (e.g. I want to change 'streetAddress' to 'address'), I don't have to make change in several places?

My naive approach is to have a bunch of constants (e.g. ADDRESS = "address" ADDRESS_STREET_ADDRESS = "streetAddress" ..etc.. ) and so if I want to change the name of one of my fields in JSON structure, I just have to make change in one place. However, this seems to be very inefficient because my constant naming would be terribly long once I reach the third or fourth layer of the JSON structure (e.g. ADDRESS_STREETADDRESS_APTNUMBER, ADDRESS_STREETADDRESS_BUILDINGNUMBER)

I am doing this in python, but any generic answer would be OK. Thanks.

Was it helpful?

Solution

Like Cameron Sparr suggested in a comment, don't have your constant names include all levels of your JSON structure. If you have the same data in multiple places, it will actually be better if you reuse the same constant. For example, suppose your JSON has a phone number included in the address:

{
    "address": {
        "streetAddress": {
            "aptnumber" : "21",
            "building_number" : "2nd", 
            "street" : "Wall Street"
        },
        "city":"New York",
        "phoneNumber":
        [
            {
                "type":"home",
                "number":"212 555-1234"
            }
        ]
    },
    "phoneNumber":
    [
        {
            "type":"home",
            "number":"212 555-1234"
        }
    ]
}

Why not have a single constant PHONES = 'phoneNumber' that you use in both places? Your constants will have shorter names, and it is more logically coherent. You would end up using it like this (assuming JSON is stored in person):

person[ADDRESS][PHONES][x] # Phone numbers associated with that address
person[PHONES][x] # Phone numbers associated with the person

Instead of

person[ADDRESS][ADDRESS_PHONES][x]
person[PHONE_NUMBERS][x]

OTHER TIPS

You can write a script than when you change the constant, change the structure in all json files.

Example:

import json
CHANGE = ('steet', 'streetAddress')
json_data = None
with open('file.json') as jfile:
    json_data = jfile.load(jfile)
    json_data[CHANGE[1]], json_data[CHANGE[0]] = json_data[CHANGE[0]], None
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top