Modifying JSON Array objects in AL

jonathanchye
Member Posts: 34
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?
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?
0
Best Answer
-
Hello @jonathanchye,
For this particular case you can do somethong like this:var jsObj: JsonObject; jsObj2: JsonObject; jsToken: JsonToken; jsToken2: JsonToken; jsToken3: JsonToken; jsToken4: JsonToken; jsArray: JsonArray; InStr: InStream; OutStr: OutStream; FileName: Text; tmpBlob: Codeunit "Temp Blob"; begin if UploadIntoStream('Load JSON File', '', '', FileName, InStr) then begin jsObj.ReadFrom(InStr); if jsObj.Get('transactions', jsToken) then begin if jsToken.IsArray then begin jsArray := jstoken.AsArray(); foreach jsToken2 in jsArray do begin if jsToken2.SelectToken('checkoutPaymentDetails', jsToken3) then if jstoken3.SelectToken('cardBin', jsToken4) then if jsToken4.IsValue then begin jsObj2 := jsToken3.AsObject(); jsObj2.Replace('cardBin', 'xxxx'); end; end; end; end; tmpBlob.CreateOutStream(OutStr); jsObj.WriteTo(OutStr); tmpBlob.CreateInStream(InStr); FileName := 'output.json'; DownloadFromStream(InStr, '', '', '', FileName); end; end;
I copy paste your json data into a file that is loaded with the UploadIntoStream function.
I am using a BC 15 version, so if this code:tmpBlob: Codeunit "Temp Blob"; tmpBlob.CreateOutStream(OutStr); tmpBlob.CreateInStream(InStr);
Doesn't work in your instalation change it to this:tmpBlob: Record TempBlob; tmpBlob.Blob.CreateOutStream(OutStr); tmpBlob.Blob.CreateInStream(InStr);
Regards
5
Answers
-
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": "bob@gmail.com", "confirmationPhone": "5047130000", "organization": "SIGNIFYD", "deliveryAddress": { "streetAddress": "123 State Street", "unit": "2A", "city": "Chicago", "provinceCode": "IL", "postalCode": "60622", "countryCode": "US" } }, { "fullName": "Mary Smith", "confirmationEmail": "mary@gmail.com", "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": "bob@gmail.com", "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" } }
0 -
Hello @jonathanchye,
For this particular case you can do somethong like this:var jsObj: JsonObject; jsObj2: JsonObject; jsToken: JsonToken; jsToken2: JsonToken; jsToken3: JsonToken; jsToken4: JsonToken; jsArray: JsonArray; InStr: InStream; OutStr: OutStream; FileName: Text; tmpBlob: Codeunit "Temp Blob"; begin if UploadIntoStream('Load JSON File', '', '', FileName, InStr) then begin jsObj.ReadFrom(InStr); if jsObj.Get('transactions', jsToken) then begin if jsToken.IsArray then begin jsArray := jstoken.AsArray(); foreach jsToken2 in jsArray do begin if jsToken2.SelectToken('checkoutPaymentDetails', jsToken3) then if jstoken3.SelectToken('cardBin', jsToken4) then if jsToken4.IsValue then begin jsObj2 := jsToken3.AsObject(); jsObj2.Replace('cardBin', 'xxxx'); end; end; end; end; tmpBlob.CreateOutStream(OutStr); jsObj.WriteTo(OutStr); tmpBlob.CreateInStream(InStr); FileName := 'output.json'; DownloadFromStream(InStr, '', '', '', FileName); end; end;
I copy paste your json data into a file that is loaded with the UploadIntoStream function.
I am using a BC 15 version, so if this code:tmpBlob: Codeunit "Temp Blob"; tmpBlob.CreateOutStream(OutStr); tmpBlob.CreateInStream(InStr);
Doesn't work in your instalation change it to this:tmpBlob: Record TempBlob; tmpBlob.Blob.CreateOutStream(OutStr); tmpBlob.Blob.CreateInStream(InStr);
Regards
5 -
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;
0 -
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;
1 -
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions