Modifying JSON Array objects in AL

I am looking at masking certain values within my JSON file.
I'm at the stage where I can a text variable containing the JSON file.
How do I read from the text variable into a JSON object, iterate through an array element and replace values of certain objects within that element?

Best Answer

Answers

  • jonathanchyejonathanchye Posts: 34Member
    I suppose it will be clearer with an example :)

    Below is the JSON. I would like to mask the following keys:
    1) cardBin
    2) cardLast4
    3) cardExpiryMonth
    4) cardExpiryYear

    What I have now is something like below but doesn't seem to do what I want.
    JSONObject.ReadFrom(Output);
    if JSONObject.Get('cardBin', JSONToken) then
         JSONObject.Set('cardBin', 'xxxx');
    

    JSON example:
    {
      "purchase": {
        "orderSessionId": "uha3d98weicm20eufhlqe",
        "orderId": "19418",
        "checkoutToken": "b1946ac92492d2347c6235b4d2611184",
        "totalPrice": 74.99,
        "products": [
          {
            "itemId": "1",
            "itemName": "Sparkly sandals",
            "itemIsDigital": false,
            "itemCategory": "apparel",
            "itemSubCategory": "footwear",
            "itemUrl": "http://mydomain.com/sparkly-sandals",
            "itemImage": "http://mydomain.com/images/sparkly-sandals.jpeg",
            "itemQuantity": 1,
            "itemPrice": 49.99,
            "itemWeight": 5
          },
          {
            "itemId": "2",
            "itemName": "Summer tank top",
            "itemIsDigital": false,
            "itemCategory": "apparel",
            "itemSubCategory": "shirts",
            "itemUrl": "http://mydomain.com/summer-tank",
            "itemImage": "http://mydomain.com/images/summer-tank.jpeg",
            "itemQuantity": 1,
            "itemPrice": 19.99,
            "itemWeight": 2
          }
        ]
      },
      "recipients": [
        {
          "fullName": "Bob Smith",
          "confirmationEmail": "[email protected]",
          "confirmationPhone": "5047130000",
          "organization": "SIGNIFYD",
          "deliveryAddress": {
            "streetAddress": "123 State Street",
            "unit": "2A",
            "city": "Chicago",
            "provinceCode": "IL",
            "postalCode": "60622",
            "countryCode": "US"
          }
        },
        {
          "fullName": "Mary Smith",
          "confirmationEmail": "[email protected]",
          "confirmationPhone": "5047120000",
          "organization": "SIGNIFYD",
          "deliveryAddress": {
            "streetAddress": "123 State Street",
            "unit": "2A",
            "city": "Chicago",
            "provinceCode": "IL",
            "postalCode": "60622",
            "countryCode": "US"
          }
        }
      ],
      "transactions": [
        {
          "parentTransactionId": null,
          "transactionId": "1053198581844",
          "createdAt": "2018-10-11T17:55:31-05:00",
          "gateway": "Stripe",
          "paymentMethod": "CREDIT_CARD",
          "type": "AUTHORIZATION",
          "gatewayStatusCode": "FAILURE",
          "gatewayStatusMessage": "incorrect CVC number provided.",
          "gatewayErrorCode": "INVALID_CVC",
          "currency": "USD",
          "amount": 700,
          "avsResponseCode": "Y",
          "cvvResponseCode": "N",
          "checkoutPaymentDetails": {
            "holderName": "John Wick",
            "cardBin": "403712",
            "cardLast4": "3423",
            "cardExpiryMonth": 12,
            "cardExpiryYear": 2021,
            "bankAccountNumber": null,
            "bankRoutingNumber": null,
            "billingAddress": {
              "streetAddress": "1800 Vine Street",
              "unit": "2A",
              "city": "Chicago",
              "provinceCode": "IL",
              "postalCode": "60622",
              "countryCode": "US"
            }
          }
        },
        {
          "parentTransactionId": "1053198581844",
          "transactionId": "1053198581844",
          "createdAt": "2018-10-11T17:56:11-05:00",
          "gateway": "Stripe",
          "paymentMethod": "CREDIT_CARD",
          "type": "AUTHORIZATION",
          "gatewayStatusCode": "SUCCESS",
          "gatewayStatusMessage": null,
          "gatewayErrorCode": null,
          "currency": "USD",
          "amount": 700,
          "avsResponseCode": "Y",
          "cvvResponseCode": "M",
          "checkoutPaymentDetails": {
            "holderName": "John Wick",
            "cardBin": "403712",
            "cardLast4": "3423",
            "cardExpiryMonth": 12,
            "cardExpiryYear": 2021,
            "bankAccountNumber": null,
            "bankRoutingNumber": null,
            "billingAddress": {
              "streetAddress": "1800 Vine Street",
              "unit": "2A",
              "city": "Chicago",
              "provinceCode": "IL",
              "postalCode": "60622",
              "countryCode": "US"
            }
          }
        },
        {
          "parentTransactionId": null,
          "transactionId": "PAY-1B56960729604235TKQQIYVY",
          "createdAt": "2018-10-11T17:55:41-05:00",
          "gateway": "Paypal Express",
          "paymentMethod": "PAYPAL_ACCOUNT",
          "type": "AUTHORIZATION",
          "gatewayStatusCode": "PENDING",
          "gatewayStatusMessage": null,
          "gatewayErrorCode": null,
          "currency": "USD",
          "amount": 100,
          "avsResponseCode": null,
          "cvvResponseCode": null,
          "paypalPendingReasonCode": "VERIFICATION_REQUIRED",
          "paypalProtectionEligibility": "ELIGIBLE",
          "paypalProtectionEligibilityType": "ITEM_NOT_RECEIVED_ELIGIBLE",
          "checkoutPaymentDetails": {
            "holderName": "John Wick",
            "cardBin": null,
            "cardLast4": null,
            "cardExpiryMonth": null,
            "cardExpiryYear": null,
            "bankAccountNumber": null,
            "bankRoutingNumber": null,
            "billingAddress": {
              "streetAddress": "1800 Vine Street",
              "unit": "2A",
              "city": "Chicago",
              "provinceCode": "IL",
              "postalCode": "60622",
              "countryCode": "US"
            }
          }
        }
      ],
      "userAccount": {
        "email": "[email protected]",
        "username": "bobbo",
        "phone": "5555551212",
        "createdDate": "2013-01-18T17:54:31-05:00",
        "accountNumber": "54321",
        "lastOrderId": "4321",
        "aggregateOrderCount": 40,
        "aggregateOrderDollars": 5000,
        "lastUpdateDate": "2013-01-18T17:54:31-05:00",
        "rating": "200"
      }
    }
    
  • jonathanchyejonathanchye Posts: 34Member
    Many thanks @ftornero !

    Edited my post and somehow it mysteriously dissapeared :(

    Anyway my question was I need to mask 3 other fields. Can I re-use the same objects? i.e:
                                if MaskJsonToken3.SelectToken('cardBin', MaskJsonToken4) then
                                    if MaskJsonToken4.IsValue then begin
                                        MaskJsonObject2 := MaskJsonToken3.AsObject();
                                        MaskJsonObject2.Replace('cardBin', '*****');
                                    end;
                                if MaskJsonToken3.SelectToken('cardLast4', MaskJsonToken4) then
                                    if MaskJsonToken4.IsValue then begin
                                        MaskJsonObject2 := MaskJsonToken3.AsObject();
                                        MaskJsonObject2.Replace('cardLast4', '****');
                                    end;
                                if MaskJsonToken3.SelectToken('cardExpiryMonth', MaskJsonToken4) then
                                    if MaskJsonToken4.IsValue then begin
                                        MaskJsonObject2 := MaskJsonToken3.AsObject();
                                        MaskJsonObject2.Replace('cardExpiryMonth', '**');
                                    end;
                                if MaskJsonToken3.SelectToken('cardExpiryYear', MaskJsonToken4) then
                                    if MaskJsonToken4.IsValue then begin
                                        MaskJsonObject2 := MaskJsonToken3.AsObject();
                                        MaskJsonObject2.Replace('cardExpiryYear', '**');
                                    end;
                        end;
    
    
  • ftorneroftornero Posts: 258Member
    Hello @jonathanchye ,

    Yes, you can do that but check out the names of the keys that you want to change because in your initial json data they are "cardExpiryMonth" not "experyMonth" and so on.
                            if jsToken2.SelectToken('checkoutPaymentDetails', jsToken3) then begin
                                if jstoken3.SelectToken('cardBin', jsToken4) then
                                    if jsToken4.IsValue then begin
                                        jsObj2 := jsToken3.AsObject();
                                        jsObj2.Replace('cardBin', 'xxxx');
                                    end;
                                if jstoken3.SelectToken('cardLast4', jsToken4) then
                                    if jsToken4.IsValue then begin
                                        jsObj2 := jsToken3.AsObject();
                                        jsObj2.Replace('cardLast4', 'xxxx');
                                    end;
                                if jstoken3.SelectToken('cardExpiryMonth', jsToken4) then
                                    if jsToken4.IsValue then begin
                                        jsObj2 := jsToken3.AsObject();
                                        jsObj2.Replace('cardExpiryMonth', 'xxxx');
                                    end;
                                if jstoken3.SelectToken('cardExpiryYear', jsToken4) then
                                    if jsToken4.IsValue then begin
                                        jsObj2 := jsToken3.AsObject();
                                        jsObj2.Replace('cardExpiryYear', 'xxxx');
                                    end;
                            end;
                        end;
    
  • jonathanchyejonathanchye Posts: 34Member
    Thanks @ftornero !
    It was a typo in my earlier reply, sorted it and all working now :)
Sign In or Register to comment.