Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json parsing and mapping keys

I'm trying to map json to send it to another applications which expects the data in it's own formats, I'm using the AWS Lambda which when an event is triggered GETs below json which needs to be parsed and mapped according to what application expects. but the key stack is so large eg "rateCode" in "ratePlan" in "Details", there are almost 20000 rate codes like "abc", "xyz",... it is not a great idea to map like

if "rateCode" == "abc":
    application_two_dict["rate_code"] = 123

so there are many more keys which keys has large set of values. what is the best way to map those keys. Also this needs to be happened in two way like when we get data from application two we need the parse the json and map the keys other way around which application one understands and vice versa.




{
    "customer": {
        "firstName": "john",
        "lastName": "doe",
        "email": "[email protected]",
        "mailingAddress": {
            "address1": "123 N 1st st",
            "address2": "789",
            "countryCode": "USA",
            "stateCode": "AZ",
            "city": "Phoenix",
            "postalCode": "34567"
        },
        "telephoneNumber": {
            "telephoneNumber": "1235456789"
        }
    },
    "paymentAccount": {
        "firstName": "john",
        "lastName": "doe",
        "paymentAccountType": "VA",
        "expirationDate": "2021-05-31",
        "billingAddress": {
            "address1": "1234 N 1st st",
            "address2": "435",
            "city": "Phoenix",
            "countryCode": "USA",
            "postalCode": "213445",
            "stateCode": "AZ"
        }
    },
    "Details": {
        "123": [{
            "quantity": 1,
            "ratePlan": {
                "rateCode": "abc",
                "DetailsList": [{
                    "CategoryCode": "1234",
                }]
            }
    }
}

I still don't have the exact format of app2 json

example json

for example

app1 json

{
 "Details": {
        "123": [{
            "quantity": 1,
            "ratePlan": {
                "rateCode": "abc",
                "DetailsList": [{
                    "CategoryCode": "1234",
                }]
            }
        }
    }   
}

app 2 json

{
    user_details_code : 123,
    quantity : [1],
    rate_plan : {
        rate_code: "xyz",
        category_code : "US_SAN"
    }
}

like image 842
michael Avatar asked Nov 25 '25 22:11

michael


1 Answers

I would try the following ways: - use two static map with rateCode as keys

{ "abc": "123", ...} and { "123": "abc", ...} and use them to get values from the other app rateCode value.

  • use a database to fetch rateCode for app2 based on app1 value. Dynamo has a very low latency and can be very effective.

Maybe you could describe more precisely the json structure of the two apps.

like image 117
Brice Miramont Avatar answered Nov 28 '25 14:11

Brice Miramont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!