Oracle Apex Pl/SQL - Canonical String from JSON Format - json

I am using Oracle Apex 22.2 and Oracle Database XE 21c on CentOS 7. I want to loop through all the nested elements in a JSON document/CLOB and achieve the Canonical String (shown below). Also, any suggestions on improving my procedure or on a different solution to handle this would be appreciated. I wrote my own procedure shown below following the algorithm shown below. All help and suggestions are really appreciated.
Algorithm Pseudo Code
function string Serialize(documentStructure)
if documentStructure is simple value type
return """ + documentStructure.value + """
end if
var serializedString = ""
foreach element in the structure:
if element is not array type
serializeString.Append (""" + element.name.uppercase + """)
serializeString.Append ( Serialize(element.value) )
end if
if element is of array type
serializeString.Append (""" + element.name.uppercase + """)
foreach array element in element:
// use below line for JSON because subelements of array in JSON do not have own names
serializeString.Append (""" + element.name.uppercase + """)
serializeString.Append ( Serialize(arrayelement.value) )
end foreach
end if
end foreach
return serializedString
end function
Procedure Code
create or replace PROCEDURE ETA_JSON_SERIALIZE (json_in IN JSON_ELEMENT_T, can_str IN OUT VARCHAR2)
IS
l_object JSON_OBJECT_T;
l_array JSON_ARRAY_T;
l_keys JSON_KEY_LIST;
l_str VARCHAR2(32767);
BEGIN
FOR i IN 1 .. json_in.get_Size LOOP
IF json_in.is_Scalar() THEN
IF json_in.is_String() THEN
l_str := '"' || json_in.to_String() || '" 1st Line Break' || CHR(10);
DBMS_OUTPUT.PUT_LINE(l_str);
RETURN;
ELSIF json_in.is_Number() THEN
l_str := '"' || json_in.to_Number() || '" 2nd Line Break' || CHR(10);
DBMS_OUTPUT.PUT_LINE(l_str);
RETURN;
ELSE
l_str := 'Other Scalar' || ' 3rd Line Break' || CHR(10);
DBMS_OUTPUT.PUT_LINE(l_str);
END IF;
ELSIF json_in.is_Object() THEN
-- l_str := 'Object:' || CHR(10);
-- DBMS_OUTPUT.PUT_LINE(l_str);
l_object := JSON_OBJECT_T( json_in );
-- l_object.on_Error(1);
l_keys := l_object.get_Keys();
-- l_str := ' Element Size: ' || json_in.get_Size || CHR(10);
-- l_str := ' Key Count: ' || l_keys.COUNT || CHR(10);
-- DBMS_OUTPUT.PUT_LINE(l_str);
l_str := UPPER(l_keys(i)) || ' 4th Line Break' || CHR(10);
DBMS_OUTPUT.PUT_LINE(l_str);
ETA_JSON_SERIALIZE( l_object.get( l_keys(i) ), l_str );
ELSIF json_in.is_Array() THEN
-- l_str := l_str || 'Array:' || CHR(10);
-- DBMS_OUTPUT.PUT_LINE(l_str);
l_array := JSON_ARRAY_T( json_in );
-- l_array.on_Error(1);
-- l_str := ' Element Size: ' || json_in.get_Size || CHR(10);
-- DBMS_OUTPUT.PUT_LINE(l_str);
-- l_str := ' Array Size: ' || l_array.get_Size || CHR(10);
-- DBMS_OUTPUT.PUT_LINE(l_str);
-- FOR j IN 0 .. l_array.get_Size - 1 LOOP
ETA_JSON_SERIALIZE( l_array.get(i - 1), l_str );
-- END LOOP;
ELSE
l_str := 'Other Type' || CHR(10) || '5th Line Break' || CHR(10);
DBMS_OUTPUT.PUT_LINE(l_str);
END IF;
END LOOP;
can_str := l_str;
END;
PL/SQL Block
SET SERVEROUTPUT ON
DECLARE
can_str VARCHAR2(32767);
l_element JSON_ELEMENT_T;
l_element1 JSON_ELEMENT_T;
l_object JSON_OBJECT_T;
l_keys JSON_KEY_LIST;
l_doc CLOB := '{a:100, b:200, c:300}';
l_doc1 CLOB := '{
"department": "Accounting",
"employees": [
{
"name": "Shelley,Higgins",
"job": "Accounting Manager"
},
{
"name": "William,Gietz",
"job": "Public Accountant"
}
]
}';
l_doc2 CLOB := '{
"REQS": {
"INDICATOR": "Y",
"NUMBER": 0,
"CATEGORY": "TU",
"ID_R": 10888,
"SUPPL_VAL": 0,
"LINE_ITEMSSUB": {
"QTY": 0,
"TOTAL_QTY": 1,
"PIPE": {
"P_CODE": 9801,
"P_ID": 7500030,
"CC_CODE": "C6AJG4"
}
}
},
"Name": "Rajesh",
"Age": 47
}';
l_doc3 CLOB := '{
"documents": [{
"issuer": {
"type": "B",
"id": "301188475",
"name": "Hakim Misr Paco (POS-0)",
"address": {
"branchID": "-1",
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "Mostafa El Nahas",
"buildingNumber": "65"
}
},
"receiver": {
"type": "P",
"id": " 29409200104255",
"name": "Karim Ahmed Abdelhakim Hashem",
"address": {
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "Mostafa El Nahas",
"buildingNumber": "65"
}
},
"documentType": "I",
"documentTypeVersion": "0.9",
"dateTimeIssued": "2022-11-19T23:59:59Z",
"taxpayerActivityCode": "2220",
"internalID": "0-1",
"invoiceLines": [{
"description": "KFC_Test",
"itemType": "GS1",
"itemCode": "10006331",
"unitType": "EA",
"quantity": 1000,
"unitValue": {
"currencySold": "EGP",
"amountEGP": 0.10000,
"amountSold": 0
},
"salesTotal": 100,
"total": 114,
"valueDifference": 0,
"totalTaxableFees": 0,
"netTotal": 100,
"itemsDiscount": 0,
"discount": {
"rate": 0,
"amount": 0
},
"taxableItems": [{
"taxType": "T1",
"amount": 14,
"subType": "V009",
"rate": 14
}],
"internalCode": "Test_110"
}],
"totalSalesAmount": 100,
"totalDiscountAmount": 0,
"netAmount": 100,
"taxTotals": [{
"taxType": "T1",
"amount": 14
}],
"extraDiscountAmount": 0,
"totalItemsDiscountAmount": 0,
"totalAmount": 114,
"signatures": [{
"signatureType": "I",
"value": "NA"
}]
}]
}';
BEGIN
l_element := JSON_ELEMENT_T.parse( l_doc3 );
ETA_JSON_SERIALIZE(l_element, can_str);
DBMS_OUTPUT.PUT_LINE(can_str);
END;
/
JSON Format
<pre>
{
"issuer": {
"address": {
"branchID": "1",
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "580 Clementina Key",
"buildingNumber": "Bldg. 0",
"postalCode": "68030",
"floor": "1",
"room": "123",
"landmark": "7660 Melody Trail",
"additionalInformation": "beside Townhall"
},
"type": "B",
"id": "113317713",
"name": "Issuer Company"
},
"receiver": {
"address": {
"country": "EG",
"governate": "Egypt",
"regionCity": "Mufazat al Ismlyah",
"street": "580 Clementina Key",
"buildingNumber": "Bldg. 0",
"postalCode": "68030",
"floor": "1",
"room": "123",
"landmark": "7660 Melody Trail",
"additionalInformation": "beside Townhall"
},
"type": "B",
"id": "313717919",
"name": "Receiver"
},
"documentType": "I",
"documentTypeVersion": "0.9",
"dateTimeIssued": "2020-10-27T23:59:59Z",
"taxpayerActivityCode": "4620",
"internalID": "IID1",
"purchaseOrderReference": "P-233-A6375",
"purchaseOrderDescription": "purchase Order description",
"salesOrderReference": "1231",
"salesOrderDescription": "Sales Order description",
"proformaInvoiceNumber": "SomeValue",
"payment": {
"bankName": "SomeValue",
"bankAddress": "SomeValue",
"bankAccountNo": "SomeValue",
"bankAccountIBAN": "",
"swiftCode": "",
"terms": "SomeValue"
},
"delivery": {
"approach": "SomeValue",
"packaging": "SomeValue",
"dateValidity": "2020-09-28T09:30:10Z",
"exportPort": "SomeValue",
"countryOfOrigin": "EG",
"grossWeight": 10.50,
"netWeight": 20.50,
"terms": "SomeValue"
},
"invoiceLines": [
{
"description": "Computer1",
"itemType": "GPC",
"itemCode": "10001774",
"unitType": "EA",
"quantity": 5,
"internalCode": "IC0",
"salesTotal": 947.00,
"total": 2969.89,
"valueDifference": 7.00,
"totalTaxableFees": 817.42,
"netTotal": 880.71,
"itemsDiscount": 5.00,
"unitValue": {
"currencySold": "EUR",
"amountEGP": 189.40,
"amountSold": 10.00,
"currencyExchangeRate": 18.94
},
"discount": {
"rate": 7,
"amount": 66.29
},
"taxableItems": [
{
"taxType": "T1",
"amount": 272.07,
"subType": "T1",
"rate": 14.00
},
{
"taxType": "T2",
"amount": 208.22,
"subType": "T2",
"rate": 12
},
{
"taxType": "T3",
"amount": 30.00,
"subType": "T3",
"rate": 0.00
},
{
"taxType": "T4",
"amount": 43.79,
"subType": "T4",
"rate": 5.00
},
{
"taxType": "T5",
"amount": 123.30,
"subType": "T5",
"rate": 14.00
},
{
"taxType": "T6",
"amount": 60.00,
"subType": "T6",
"rate": 0.00
},
{
"taxType": "T7",
"amount": 88.07,
"subType": "T7",
"rate": 10.00
},
{
"taxType": "T8",
"amount": 123.30,
"subType": "T8",
"rate": 14.00
},
{
"taxType": "T9",
"amount": 105.69,
"subType": "T9",
"rate": 12.00
},
{
"taxType": "T10",
"amount": 88.07,
"subType": "T10",
"rate": 10.00
},
{
"taxType": "T11",
"amount": 123.30,
"subType": "T11",
"rate": 14.00
},
{
"taxType": "T12",
"amount": 105.69,
"subType": "T12",
"rate": 12.00
},
{
"taxType": "T13",
"amount": 88.07,
"subType": "T13",
"rate": 10.00
},
{
"taxType": "T14",
"amount": 123.30,
"subType": "T14",
"rate": 14.00
},
{
"taxType": "T15",
"amount": 105.69,
"subType": "T15",
"rate": 12.00
},
{
"taxType": "T16",
"amount": 88.07,
"subType": "T16",
"rate": 10.00
},
{
"taxType": "T17",
"amount": 88.07,
"subType": "T17",
"rate": 10.00
},
{
"taxType": "T18",
"amount": 123.30,
"subType": "T18",
"rate": 14.00
},
{
"taxType": "T19",
"amount": 105.69,
"subType": "T19",
"rate": 12.00
},
{
"taxType": "T20",
"amount": 88.07,
"subType": "T20",
"rate": 10.00
}
]
},
{
"description": "Computer2",
"itemType": "GPC",
"itemCode": "10003752",
"unitType": "EA",
"quantity": 7,
"internalCode": "IC0",
"salesTotal": 662.90,
"total": 2226.61,
"valueDifference": 6.00,
"totalTaxableFees": 621.51,
"netTotal": 652.90,
"itemsDiscount": 9.00,
"unitValue": {
"currencySold": "EUR",
"amountEGP": 94.70,
"amountSold": 5.00,
"currencyExchangeRate": 18.94
},
"discount": {
"rate": 0,
"amount": 10.00
},
"taxableItems": [
{
"taxType": "T1",
"amount": 205.47,
"subType": "T1",
"rate": 14.00
},
{
"taxType": "T2",
"amount": 157.25,
"subType": "T2",
"rate": 12
},
{
"taxType": "T3",
"amount": 30.00,
"subType": "T3",
"rate": 0.00
},
{
"taxType": "T4",
"amount": 32.20,
"subType": "T4",
"rate": 5.00
},
{
"taxType": "T5",
"amount": 91.41,
"subType": "T5",
"rate": 14.00
},
{
"taxType": "T6",
"amount": 60.00,
"subType": "T6",
"rate": 0.00
},
{
"taxType": "T7",
"amount": 65.29,
"subType": "T7",
"rate": 10.00
},
{
"taxType": "T8",
"amount": 91.41,
"subType": "T8",
"rate": 14.00
},
{
"taxType": "T9",
"amount": 78.35,
"subType": "T9",
"rate": 12.00
},
{
"taxType": "T10",
"amount": 65.29,
"subType": "T10",
"rate": 10.00
},
{
"taxType": "T11",
"amount": 91.41,
"subType": "T11",
"rate": 14.00
},
{
"taxType": "T12",
"amount": 78.35,
"subType": "T12",
"rate": 12.00
},
{
"taxType": "T13",
"amount": 65.29,
"subType": "T13",
"rate": 10.00
},
{
"taxType": "T14",
"amount": 91.41,
"subType": "T14",
"rate": 14.00
},
{
"taxType": "T15",
"amount": 78.35,
"subType": "T15",
"rate": 12.00
},
{
"taxType": "T16",
"amount": 65.29,
"subType": "T16",
"rate": 10.00
},
{
"taxType": "T17",
"amount": 65.29,
"subType": "T17",
"rate": 10.00
},
{
"taxType": "T18",
"amount": 91.41,
"subType": "T18",
"rate": 14.00
},
{
"taxType": "T19",
"amount": 78.35,
"subType": "T19",
"rate": 12.00
},
{
"taxType": "T20",
"amount": 65.29,
"subType": "T20",
"rate": 10.00
}
]
}
],
"totalDiscountAmount": 76.29,
"totalSalesAmount": 1609.90,
"netAmount": 1533.61,
"taxTotals": [
{
"taxType": "T1",
"amount": 477.54
},
{
"taxType": "T2",
"amount": 365.47
},
{
"taxType": "T3",
"amount": 60.00
},
{
"taxType": "T4",
"amount": 75.99
},
{
"taxType": "T5",
"amount": 214.71
},
{
"taxType": "T6",
"amount": 120.00
},
{
"taxType": "T7",
"amount": 153.36
},
{
"taxType": "T8",
"amount": 214.71
},
{
"taxType": "T9",
"amount": 184.04
},
{
"taxType": "T10",
"amount": 153.36
},
{
"taxType": "T11",
"amount": 214.71
},
{
"taxType": "T12",
"amount": 184.04
},
{
"taxType": "T13",
"amount": 153.36
},
{
"taxType": "T14",
"amount": 214.71
},
{
"taxType": "T15",
"amount": 184.04
},
{
"taxType": "T16",
"amount": 153.36
},
{
"taxType": "T17",
"amount": 153.36
},
{
"taxType": "T18",
"amount": 214.71
},
{
"taxType": "T19",
"amount": 184.04
},
{
"taxType": "T20",
"amount": 153.36
}
],
"totalAmount": 5191.50,
"extraDiscountAmount": 5.00,
"totalItemsDiscountAmount": 14.00
}
Canonical String
"ISSUER""ADDRESS""BRANCHID""1""COUNTRY""EG""GOVERNATE""Cairo""REGIONCITY""Nasr City""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""113317713""NAME""Issuer Company""RECEIVER""ADDRESS""COUNTRY""EG""GOVERNATE""Egypt""REGIONCITY""Mufazat al Ismlyah""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""313717919""NAME""Receiver""DOCUMENTTYPE""I""DOCUMENTTYPEVERSION""0.9""DATETIMEISSUED""2020-10-27T23:59:59Z""TAXPAYERACTIVITYCODE""4620""INTERNALID""IID1""PURCHASEORDERREFERENCE""P-233-A6375""PURCHASEORDERDESCRIPTION""purchase Order description""SALESORDERREFERENCE""1231""SALESORDERDESCRIPTION""Sales Order description""PROFORMAINVOICENUMBER""SomeValue""PAYMENT""BANKNAME""SomeValue""BANKADDRESS""SomeValue""BANKACCOUNTNO""SomeValue""BANKACCOUNTIBAN""""SWIFTCODE""""TERMS""SomeValue""DELIVERY""APPROACH""SomeValue""PACKAGING""SomeValue""DATEVALIDITY""2020-09-28T09:30:10Z""EXPORTPORT""SomeValue""COUNTRYOFORIGIN""EG""GROSSWEIGHT""10.50""NETWEIGHT""20.50""TERMS""SomeValue""INVOICELINES""INVOICELINES""DESCRIPTION""Computer1""ITEMTYPE""GPC""ITEMCODE""10001774""UNITTYPE""EA""QUANTITY""5""INTERNALCODE""IC0""SALESTOTAL""947.00""TOTAL""2969.89""VALUEDIFFERENCE""7.00""TOTALTAXABLEFEES""817.42""NETTOTAL""880.71""ITEMSDISCOUNT""5.00""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""189.40""AMOUNTSOLD""10.00""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""7""AMOUNT""66.29""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""272.07""SUBTYPE""T1""RATE""14.00""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""208.22""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30.00""SUBTYPE""T3""RATE""0.00""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""43.79""SUBTYPE""T4""RATE""5.00""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""123.30""SUBTYPE""T5""RATE""14.00""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60.00""SUBTYPE""T6""RATE""0.00""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""88.07""SUBTYPE""T7""RATE""10.00""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""123.30""SUBTYPE""T8""RATE""14.00""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""105.69""SUBTYPE""T9""RATE""12.00""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""88.07""SUBTYPE""T10""RATE""10.00""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""123.30""SUBTYPE""T11""RATE""14.00""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""105.69""SUBTYPE""T12""RATE""12.00""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""88.07""SUBTYPE""T13""RATE""10.00""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""123.30""SUBTYPE""T14""RATE""14.00""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""105.69""SUBTYPE""T15""RATE""12.00""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""88.07""SUBTYPE""T16""RATE""10.00""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""88.07""SUBTYPE""T17""RATE""10.00""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""123.30""SUBTYPE""T18""RATE""14.00""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""105.69""SUBTYPE""T19""RATE""12.00""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""88.07""SUBTYPE""T20""RATE""10.00""INVOICELINES""DESCRIPTION""Computer2""ITEMTYPE""GPC""ITEMCODE""10003752""UNITTYPE""EA""QUANTITY""7""INTERNALCODE""IC0""SALESTOTAL""662.90""TOTAL""2226.61""VALUEDIFFERENCE""6.00""TOTALTAXABLEFEES""621.51""NETTOTAL""652.90""ITEMSDISCOUNT""9.00""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""94.70""AMOUNTSOLD""5.00""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""0""AMOUNT""10.00""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""205.47""SUBTYPE""T1""RATE""14.00""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""157.25""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30.00""SUBTYPE""T3""RATE""0.00""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""32.20""SUBTYPE""T4""RATE""5.00""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""91.41""SUBTYPE""T5""RATE""14.00""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60.00""SUBTYPE""T6""RATE""0.00""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""65.29""SUBTYPE""T7""RATE""10.00""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""91.41""SUBTYPE""T8""RATE""14.00""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""78.35""SUBTYPE""T9""RATE""12.00""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""65.29""SUBTYPE""T10""RATE""10.00""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""91.41""SUBTYPE""T11""RATE""14.00""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""78.35""SUBTYPE""T12""RATE""12.00""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""65.29""SUBTYPE""T13""RATE""10.00""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""91.41""SUBTYPE""T14""RATE""14.00""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""78.35""SUBTYPE""T15""RATE""12.00""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""65.29""SUBTYPE""T16""RATE""10.00""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""65.29""SUBTYPE""T17""RATE""10.00""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""91.41""SUBTYPE""T18""RATE""14.00""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""78.35""SUBTYPE""T19""RATE""12.00""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""65.29""SUBTYPE""T20""RATE""10.00""TOTALDISCOUNTAMOUNT""76.29""TOTALSALESAMOUNT""1609.90""NETAMOUNT""1533.61""TAXTOTALS""TAXTOTALS""TAXTYPE""T1""AMOUNT""477.54""TAXTOTALS""TAXTYPE""T2""AMOUNT""365.47""TAXTOTALS""TAXTYPE""T3""AMOUNT""60.00""TAXTOTALS""TAXTYPE""T4""AMOUNT""75.99""TAXTOTALS""TAXTYPE""T5""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T6""AMOUNT""120.00""TAXTOTALS""TAXTYPE""T7""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T8""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T9""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T10""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T11""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T12""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T13""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T14""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T15""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T16""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T17""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T18""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T19""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T20""AMOUNT""153.36""TOTALAMOUNT""5191.50""EXTRADISCOUNTAMOUNT""5.00""TOTALITEMSDISCOUNTAMOUNT""14.00"

You can use:
create or replace PROCEDURE ETA_JSON_SERIALIZE (
json_in IN JSON_ELEMENT_T,
can_str IN OUT VARCHAR2,
object_key IN VARCHAR2 DEFAULT NULL
)
IS
BEGIN
IF json_in.is_Object() THEN
DECLARE
l_object JSON_OBJECT_T := TREAT(json_in AS JSON_OBJECT_T);
l_keys JSON_KEY_LIST := l_object.get_Keys();
BEGIN
FOR i IN 1 .. l_keys.COUNT LOOP
can_str := can_str || '"'||UPPER(l_keys(i))||'"';
ETA_JSON_SERIALIZE(l_object.get(l_keys(i)), can_str, l_keys(i));
END LOOP;
END;
ELSIF json_in.is_Array() THEN
DECLARE
l_array JSON_ARRAY_T := TREAT(json_in AS JSON_ARRAY_T);
BEGIN
FOR i IN 0 .. l_array.get_size -1 LOOP
IF i > 0 AND object_key IS NOT NULL THEN
can_str := can_str || '"'||UPPER(object_key)||'"';
END IF;
ETA_JSON_SERIALIZE(l_array.get(i), can_str);
END LOOP;
END;
ELSIF json_in.is_Scalar() THEN
can_str := can_str || json_in.to_String();
END IF;
END;
/
Which, for the sample data:
DECLARE
can_str VARCHAR2(32767);
l_element JSON_ELEMENT_T;
l_doc CLOB := '{
"documents": [{
"issuer": {
"type": "B",
"id": "301188475",
"name": "Hakim Misr Paco (POS-0)",
"address": {
"branchID": "-1",
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "Mostafa El Nahas",
"buildingNumber": "65"
}
},
"receiver": {
"type": "P",
"id": " 29409200104255",
"name": "Karim Ahmed Abdelhakim Hashem",
"address": {
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "Mostafa El Nahas",
"buildingNumber": "65"
}
},
"documentType": "I",
"documentTypeVersion": "0.9",
"dateTimeIssued": "2022-11-19T23:59:59Z",
"taxpayerActivityCode": "2220",
"internalID": "0-1",
"invoiceLines": [{
"description": "KFC_Test",
"itemType": "GS1",
"itemCode": "10006331",
"unitType": "EA",
"quantity": 1000,
"unitValue": {
"currencySold": "EGP",
"amountEGP": 0.10000,
"amountSold": 0
},
"salesTotal": 100,
"total": 114,
"valueDifference": 0,
"totalTaxableFees": 0,
"netTotal": 100,
"itemsDiscount": 0,
"discount": {
"rate": 0,
"amount": 0
},
"taxableItems": [{
"taxType": "T1",
"amount": 14,
"subType": "V009",
"rate": 14
}],
"internalCode": "Test_110"
}],
"totalSalesAmount": 100,
"totalDiscountAmount": 0,
"netAmount": 100,
"taxTotals": [{
"taxType": "T1",
"amount": 14
}],
"extraDiscountAmount": 0,
"totalItemsDiscountAmount": 0,
"totalAmount": 114,
"signatures": [{
"signatureType": "I",
"value": "NA"
}]
}]
}';
BEGIN
ETA_JSON_SERIALIZE(JSON_ELEMENT_T.parse( l_doc ), can_str);
DBMS_OUTPUT.PUT_LINE(can_str);
END;
/
Outputs:
"DOCUMENTS""ISSUER""TYPE""B""ID""301188475""NAME""Hakim Misr Paco (POS-0)""ADDRESS""BRANCHID""-1""COUNTRY""EG""GOVERNATE""Cairo""REGIONCITY""Nasr City""STREET""Mostafa El Nahas""BUILDINGNUMBER""65""RECEIVER""TYPE""P""ID"" 29409200104255""NAME""Karim Ahmed Abdelhakim Hashem""ADDRESS""COUNTRY""EG""GOVERNATE""Cairo""REGIONCITY""Nasr City""STREET""Mostafa El Nahas""BUILDINGNUMBER""65""DOCUMENTTYPE""I""DOCUMENTTYPEVERSION""0.9""DATETIMEISSUED""2022-11-19T23:59:59Z""TAXPAYERACTIVITYCODE""2220""INTERNALID""0-1""INVOICELINES""DESCRIPTION""KFC_Test""ITEMTYPE""GS1""ITEMCODE""10006331""UNITTYPE""EA""QUANTITY"1000"UNITVALUE""CURRENCYSOLD""EGP""AMOUNTEGP"0.10000"AMOUNTSOLD"0"SALESTOTAL"100"TOTAL"114"VALUEDIFFERENCE"0"TOTALTAXABLEFEES"0"NETTOTAL"100"ITEMSDISCOUNT"0"DISCOUNT""RATE"0"AMOUNT"0"TAXABLEITEMS""TAXTYPE""T1""AMOUNT"14"SUBTYPE""V009""RATE"14"INTERNALCODE""Test_110""TOTALSALESAMOUNT"100"TOTALDISCOUNTAMOUNT"0"NETAMOUNT"100"TAXTOTALS""TAXTYPE""T1""AMOUNT"14"EXTRADISCOUNTAMOUNT"0"TOTALITEMSDISCOUNTAMOUNT"0"TOTALAMOUNT"114"SIGNATURES""SIGNATURETYPE""I""VALUE""NA"
fiddle

Related

Sum the duration of an member in nested json array in posgres

need an help
I need to sum the duration of the days for an particular id in an nested jason through postgres query
--Sample data
[{
"id": 55,
"r_id": 2,
"friday": null,
"monday": {
"dia_id": "wsd",
"book_on": "12:00",
"turn on": 2,
"book_off": "5:00",
"duration": "5:00"},
"sunday": {
"dia_id": "asd",
"book_on": "12:00",
"turn on": 1,
"book_off": "1:00",
"duration": "2:00"},
"tuesday": null,
"saturday": null,
"thursday": null,
"wenseday": null,
"rownumber": 48,
"insert_time": null,
"update_time": null},
{
"id": 56,
"r_id": 2,
"friday": null,
"monday": {
"dia_id": "csd",
"book_on": "12:00",
"turn on": 4,
"book_off": "4:00",
"duration": "4:00"},
"sunday": {
"dia_id": "psd",
"book_on": "12:00",
"turn on": 3,
"book_off": "5:00",
"duration": "5:00"},
"tuesday": null,
"saturday": null,
"thursday": null,
"wenseday": null,
"rownumber": 49,
"insert_time": null,
"update_time": null}
]
The output should be in this format
id duration
55 7:00
56 9:00
Thanks
WITH t AS (
SELECT
jsonb_path_query(t.*, '$.id') AS id,
s::text::interval AS duration
FROM
jsonb_array_elements('[{
"id": 55,
"r_id": 2,
"friday": null,
"monday": {
"dia_id": "wsd",
"book_on": "12:00",
"turn on": 2,
"book_off": "5:00",
"duration": "5:00"},
"sunday": {
"dia_id": "asd",
"book_on": "12:00",
"turn on": 1,
"book_off": "1:00",
"duration": "2:00"},
"tuesday": null,
"saturday": null,
"thursday": null,
"wenseday": null,
"rownumber": 48,
"insert_time": null,
"update_time": null},
{
"id": 56,
"r_id": 2,
"friday": null,
"monday": {
"dia_id": "csd",
"book_on": "12:00",
"turn on": 4,
"book_off": "4:00",
"duration": "4:00"},
"sunday": {
"dia_id": "psd",
"book_on": "12:00",
"turn on": 3,
"book_off": "5:00",
"duration": "5:00"},
"tuesday": null,
"saturday": null,
"thursday": null,
"wenseday": null,
"rownumber": 49,
"insert_time": null,
"update_time": null}
]
'::jsonb) AS t,
LATERAL jsonb_path_query(t.*, '$.*.duration') AS s
)
SELECT
id,
sum(duration)
FROM
t
GROUP BY
id
ORDER BY
id;
id | sum
----+----------
55 | 07:00:00
56 | 09:00:00

Reserve Number Format from Original JSON Data

I am using Oracle Apex 22.2 and Oracle Database XE 21c on CentOS7. I am traversing all the nested elements of JSON data with the procedure shown below. However when I get the value of a number, the number loses its original format. For example, instead of getting 100.0, I get 100. How can I reserve the original format of the number? As you can see in my code below, I am using to_String to get value of the number. This is was one of my attempts to reserve the original number format. I have also tried with TO_CHAR & TO_NUMBER() . All give the same output. Your help is really appreciated.
Procedure Code
create or replace PROCEDURE ETA_JSON_SERIALIZE (
json_in IN JSON_ELEMENT_T,
can_str IN OUT VARCHAR2,
object_key IN VARCHAR2 DEFAULT NULL
)
IS
BEGIN
IF json_in.is_Object() THEN
DECLARE
l_object JSON_OBJECT_T := TREAT(json_in AS JSON_OBJECT_T);
l_keys JSON_KEY_LIST := l_object.get_Keys();
BEGIN
FOR i IN 1 .. l_keys.COUNT LOOP
can_str := can_str || '"'||UPPER(l_keys(i))||'"';
ETA_JSON_SERIALIZE(l_object.get(l_keys(i)), can_str, l_keys(i));
END LOOP;
END;
ELSIF json_in.is_Array() THEN
DECLARE
l_array JSON_ARRAY_T := TREAT(json_in AS JSON_ARRAY_T);
BEGIN
FOR i IN 0 .. l_array.get_size - 1 LOOP
IF object_key IS NOT NULL THEN
can_str := can_str || '"'||UPPER(object_key)||'"';
END IF;
ETA_JSON_SERIALIZE(l_array.get(i), can_str);
END LOOP;
END;
ELSIF json_in.is_Scalar() THEN
IF json_in.is_String() THEN
can_str := can_str || json_in.to_String();
ELSIF json_in.is_Number() THEN
can_str := can_str || '"' || TO_NUMBER(json_in.to_String) || '"';
END IF;
END IF;
END;
Sample Code - PL/SQL Block
SET SERVEROUTPUT ON
DECLARE
can_str VARCHAR2(32767);
l_doc CLOB := '{
"issuer": {
"address": {
"branchID": "1",
"country": "EG",
"governate": "Cairo",
"regionCity": "Nasr City",
"street": "580 Clementina Key",
"buildingNumber": "Bldg. 0",
"postalCode": "68030",
"floor": "1",
"room": "123",
"landmark": "7660 Melody Trail",
"additionalInformation": "beside Townhall"
},
"type": "B",
"id": "113317713",
"name": "Issuer Company"
},
"receiver": {
"address": {
"country": "EG",
"governate": "Egypt",
"regionCity": "Mufazat al Ismlyah",
"street": "580 Clementina Key",
"buildingNumber": "Bldg. 0",
"postalCode": "68030",
"floor": "1",
"room": "123",
"landmark": "7660 Melody Trail",
"additionalInformation": "beside Townhall"
},
"type": "B",
"id": "313717919",
"name": "Receiver"
},
"documentType": "I",
"documentTypeVersion": "0.9",
"dateTimeIssued": "2020-10-27T23:59:59Z",
"taxpayerActivityCode": "4620",
"internalID": "IID1",
"purchaseOrderReference": "P-233-A6375",
"purchaseOrderDescription": "purchase Order description",
"salesOrderReference": "1231",
"salesOrderDescription": "Sales Order description",
"proformaInvoiceNumber": "SomeValue",
"payment": {
"bankName": "SomeValue",
"bankAddress": "SomeValue",
"bankAccountNo": "SomeValue",
"bankAccountIBAN": "",
"swiftCode": "",
"terms": "SomeValue"
},
"delivery": {
"approach": "SomeValue",
"packaging": "SomeValue",
"dateValidity": "2020-09-28T09:30:10Z",
"exportPort": "SomeValue",
"countryOfOrigin": "EG",
"grossWeight": 10.50,
"netWeight": 20.50,
"terms": "SomeValue"
},
"invoiceLines": [
{
"description": "Computer1",
"itemType": "GPC",
"itemCode": "10001774",
"unitType": "EA",
"quantity": 5,
"internalCode": "IC0",
"salesTotal": 947.00,
"total": 2969.89,
"valueDifference": 7.00,
"totalTaxableFees": 817.42,
"netTotal": 880.71,
"itemsDiscount": 5.00,
"unitValue": {
"currencySold": "EUR",
"amountEGP": 189.40,
"amountSold": 10.00,
"currencyExchangeRate": 18.94
},
"discount": {
"rate": 7,
"amount": 66.29
},
"taxableItems": [
{
"taxType": "T1",
"amount": 272.07,
"subType": "T1",
"rate": 14.00
},
{
"taxType": "T2",
"amount": 208.22,
"subType": "T2",
"rate": 12
},
{
"taxType": "T3",
"amount": 30.00,
"subType": "T3",
"rate": 0.00
},
{
"taxType": "T4",
"amount": 43.79,
"subType": "T4",
"rate": 5.00
},
{
"taxType": "T5",
"amount": 123.30,
"subType": "T5",
"rate": 14.00
},
{
"taxType": "T6",
"amount": 60.00,
"subType": "T6",
"rate": 0.00
},
{
"taxType": "T7",
"amount": 88.07,
"subType": "T7",
"rate": 10.00
},
{
"taxType": "T8",
"amount": 123.30,
"subType": "T8",
"rate": 14.00
},
{
"taxType": "T9",
"amount": 105.69,
"subType": "T9",
"rate": 12.00
},
{
"taxType": "T10",
"amount": 88.07,
"subType": "T10",
"rate": 10.00
},
{
"taxType": "T11",
"amount": 123.30,
"subType": "T11",
"rate": 14.00
},
{
"taxType": "T12",
"amount": 105.69,
"subType": "T12",
"rate": 12.00
},
{
"taxType": "T13",
"amount": 88.07,
"subType": "T13",
"rate": 10.00
},
{
"taxType": "T14",
"amount": 123.30,
"subType": "T14",
"rate": 14.00
},
{
"taxType": "T15",
"amount": 105.69,
"subType": "T15",
"rate": 12.00
},
{
"taxType": "T16",
"amount": 88.07,
"subType": "T16",
"rate": 10.00
},
{
"taxType": "T17",
"amount": 88.07,
"subType": "T17",
"rate": 10.00
},
{
"taxType": "T18",
"amount": 123.30,
"subType": "T18",
"rate": 14.00
},
{
"taxType": "T19",
"amount": 105.69,
"subType": "T19",
"rate": 12.00
},
{
"taxType": "T20",
"amount": 88.07,
"subType": "T20",
"rate": 10.00
}
]
},
{
"description": "Computer2",
"itemType": "GPC",
"itemCode": "10003752",
"unitType": "EA",
"quantity": 7,
"internalCode": "IC0",
"salesTotal": 662.90,
"total": 2226.61,
"valueDifference": 6.00,
"totalTaxableFees": 621.51,
"netTotal": 652.90,
"itemsDiscount": 9.00,
"unitValue": {
"currencySold": "EUR",
"amountEGP": 94.70,
"amountSold": 5.00,
"currencyExchangeRate": 18.94
},
"discount": {
"rate": 0,
"amount": 10.00
},
"taxableItems": [
{
"taxType": "T1",
"amount": 205.47,
"subType": "T1",
"rate": 14.00
},
{
"taxType": "T2",
"amount": 157.25,
"subType": "T2",
"rate": 12
},
{
"taxType": "T3",
"amount": 30.00,
"subType": "T3",
"rate": 0.00
},
{
"taxType": "T4",
"amount": 32.20,
"subType": "T4",
"rate": 5.00
},
{
"taxType": "T5",
"amount": 91.41,
"subType": "T5",
"rate": 14.00
},
{
"taxType": "T6",
"amount": 60.00,
"subType": "T6",
"rate": 0.00
},
{
"taxType": "T7",
"amount": 65.29,
"subType": "T7",
"rate": 10.00
},
{
"taxType": "T8",
"amount": 91.41,
"subType": "T8",
"rate": 14.00
},
{
"taxType": "T9",
"amount": 78.35,
"subType": "T9",
"rate": 12.00
},
{
"taxType": "T10",
"amount": 65.29,
"subType": "T10",
"rate": 10.00
},
{
"taxType": "T11",
"amount": 91.41,
"subType": "T11",
"rate": 14.00
},
{
"taxType": "T12",
"amount": 78.35,
"subType": "T12",
"rate": 12.00
},
{
"taxType": "T13",
"amount": 65.29,
"subType": "T13",
"rate": 10.00
},
{
"taxType": "T14",
"amount": 91.41,
"subType": "T14",
"rate": 14.00
},
{
"taxType": "T15",
"amount": 78.35,
"subType": "T15",
"rate": 12.00
},
{
"taxType": "T16",
"amount": 65.29,
"subType": "T16",
"rate": 10.00
},
{
"taxType": "T17",
"amount": 65.29,
"subType": "T17",
"rate": 10.00
},
{
"taxType": "T18",
"amount": 91.41,
"subType": "T18",
"rate": 14.00
},
{
"taxType": "T19",
"amount": 78.35,
"subType": "T19",
"rate": 12.00
},
{
"taxType": "T20",
"amount": 65.29,
"subType": "T20",
"rate": 10.00
}
]
}
],
"totalDiscountAmount": 76.29,
"totalSalesAmount": 1609.90,
"netAmount": 1533.61,
"taxTotals": [
{
"taxType": "T1",
"amount": 477.54
},
{
"taxType": "T2",
"amount": 365.47
},
{
"taxType": "T3",
"amount": 60.00
},
{
"taxType": "T4",
"amount": 75.99
},
{
"taxType": "T5",
"amount": 214.71
},
{
"taxType": "T6",
"amount": 120.00
},
{
"taxType": "T7",
"amount": 153.36
},
{
"taxType": "T8",
"amount": 214.71
},
{
"taxType": "T9",
"amount": 184.04
},
{
"taxType": "T10",
"amount": 153.36
},
{
"taxType": "T11",
"amount": 214.71
},
{
"taxType": "T12",
"amount": 184.04
},
{
"taxType": "T13",
"amount": 153.36
},
{
"taxType": "T14",
"amount": 214.71
},
{
"taxType": "T15",
"amount": 184.04
},
{
"taxType": "T16",
"amount": 153.36
},
{
"taxType": "T17",
"amount": 153.36
},
{
"taxType": "T18",
"amount": 214.71
},
{
"taxType": "T19",
"amount": 184.04
},
{
"taxType": "T20",
"amount": 153.36
}
],
"totalAmount": 5191.50,
"extraDiscountAmount": 5.00,
"totalItemsDiscountAmount": 14.00
}';
BEGIN
ETA_JSON_SERIALIZE(JSON_ELEMENT_T.parse( l_doc ), can_str);
DBMS_OUTPUT.PUT_LINE(can_str);
END;
/
Output
"ISSUER""ADDRESS""BRANCHID""1""COUNTRY""EG""GOVERNATE""Cairo""REGIONCITY""Nasr City""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""113317713""NAME""Issuer Company""RECEIVER""ADDRESS""COUNTRY""EG""GOVERNATE""Egypt""REGIONCITY""Mufazat al Ismlyah""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""313717919""NAME""Receiver""DOCUMENTTYPE""I""DOCUMENTTYPEVERSION""0.9""DATETIMEISSUED""2020-10-27T23:59:59Z""TAXPAYERACTIVITYCODE""4620""INTERNALID""IID1""PURCHASEORDERREFERENCE""P-233-A6375""PURCHASEORDERDESCRIPTION""purchase Order description""SALESORDERREFERENCE""1231""SALESORDERDESCRIPTION""Sales Order description""PROFORMAINVOICENUMBER""SomeValue""PAYMENT""BANKNAME""SomeValue""BANKADDRESS""SomeValue""BANKACCOUNTNO""SomeValue""BANKACCOUNTIBAN""""SWIFTCODE""""TERMS""SomeValue""DELIVERY""APPROACH""SomeValue""PACKAGING""SomeValue""DATEVALIDITY""2020-09-28T09:30:10Z""EXPORTPORT""SomeValue""COUNTRYOFORIGIN""EG""GROSSWEIGHT""10.5""NETWEIGHT""20.5""TERMS""SomeValue""INVOICELINES""INVOICELINES""DESCRIPTION""Computer1""ITEMTYPE""GPC""ITEMCODE""10001774""UNITTYPE""EA""QUANTITY""5""INTERNALCODE""IC0""SALESTOTAL""947""TOTAL""2969.89""VALUEDIFFERENCE""7""TOTALTAXABLEFEES""817.42""NETTOTAL""880.71""ITEMSDISCOUNT""5""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""189.4""AMOUNTSOLD""10""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""7""AMOUNT""66.29""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""272.07""SUBTYPE""T1""RATE""14""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""208.22""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30""SUBTYPE""T3""RATE""0""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""43.79""SUBTYPE""T4""RATE""5""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""123.3""SUBTYPE""T5""RATE""14""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60""SUBTYPE""T6""RATE""0""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""88.07""SUBTYPE""T7""RATE""10""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""123.3""SUBTYPE""T8""RATE""14""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""105.69""SUBTYPE""T9""RATE""12""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""88.07""SUBTYPE""T10""RATE""10""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""123.3""SUBTYPE""T11""RATE""14""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""105.69""SUBTYPE""T12""RATE""12""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""88.07""SUBTYPE""T13""RATE""10""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""123.3""SUBTYPE""T14""RATE""14""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""105.69""SUBTYPE""T15""RATE""12""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""88.07""SUBTYPE""T16""RATE""10""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""88.07""SUBTYPE""T17""RATE""10""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""123.3""SUBTYPE""T18""RATE""14""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""105.69""SUBTYPE""T19""RATE""12""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""88.07""SUBTYPE""T20""RATE""10""INVOICELINES""DESCRIPTION""Computer2""ITEMTYPE""GPC""ITEMCODE""10003752""UNITTYPE""EA""QUANTITY""7""INTERNALCODE""IC0""SALESTOTAL""662.9""TOTAL""2226.61""VALUEDIFFERENCE""6""TOTALTAXABLEFEES""621.51""NETTOTAL""652.9""ITEMSDISCOUNT""9""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""94.7""AMOUNTSOLD""5""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""0""AMOUNT""10""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""205.47""SUBTYPE""T1""RATE""14""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""157.25""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30""SUBTYPE""T3""RATE""0""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""32.2""SUBTYPE""T4""RATE""5""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""91.41""SUBTYPE""T5""RATE""14""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60""SUBTYPE""T6""RATE""0""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""65.29""SUBTYPE""T7""RATE""10""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""91.41""SUBTYPE""T8""RATE""14""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""78.35""SUBTYPE""T9""RATE""12""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""65.29""SUBTYPE""T10""RATE""10""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""91.41""SUBTYPE""T11""RATE""14""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""78.35""SUBTYPE""T12""RATE""12""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""65.29""SUBTYPE""T13""RATE""10""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""91.41""SUBTYPE""T14""RATE""14""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""78.35""SUBTYPE""T15""RATE""12""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""65.29""SUBTYPE""T16""RATE""10""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""65.29""SUBTYPE""T17""RATE""10""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""91.41""SUBTYPE""T18""RATE""14""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""78.35""SUBTYPE""T19""RATE""12""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""65.29""SUBTYPE""T20""RATE""10""TOTALDISCOUNTAMOUNT""76.29""TOTALSALESAMOUNT""1609.9""NETAMOUNT""1533.61""TAXTOTALS""TAXTOTALS""TAXTYPE""T1""AMOUNT""477.54""TAXTOTALS""TAXTYPE""T2""AMOUNT""365.47""TAXTOTALS""TAXTYPE""T3""AMOUNT""60""TAXTOTALS""TAXTYPE""T4""AMOUNT""75.99""TAXTOTALS""TAXTYPE""T5""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T6""AMOUNT""120""TAXTOTALS""TAXTYPE""T7""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T8""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T9""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T10""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T11""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T12""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T13""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T14""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T15""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T16""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T17""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T18""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T19""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T20""AMOUNT""153.36""TOTALAMOUNT""5191.5""EXTRADISCOUNTAMOUNT""5""TOTALITEMSDISCOUNTAMOUNT""14"
Output Instances
How to make it reserve the original number format?
So "EXTRADISCOUNTAMOUNT""5.00"instead of"EXTRADISCOUNTAMOUNT""5"`
OR
"NETWEIGHT""20.50" instead of "NETWEIGHT""20.5"
OR
"TOTALITEMSDISCOUNTAMOUNT""14.00" instead of "TOTALITEMSDISCOUNTAMOUNT""14"
OR
"RATE""0.00" instead of "RATE""0"
You cannot.
Once you have parsed the JSON then Oracle has converted it to JSON_ELEMENT_T then there is no documented method that allows you to extract the underlying JSON text for that element so you can only use the serialization methods:
Serialization
Serialization is the inverse of the parse function. The serialization operation takes the in-memory representation of the JSON data and prints it to a string. The serialization functions and procedures are:
MEMBER FUNCTION to_String RETURN VARCHAR2
MEMBER FUNCTION to_Number RETURN NUMBER
MEMBER FUNCTION to_Date RETURN DATE
MEMBER FUNCTION to_Timestamp RETURN TIMESTAMP
MEMBER FUNCTION to_Boolean RETURN BOOLEAN
MEMBER FUNCTION to_Clob RETURN CLOB
MEMBER FUNCTION to_Blob RETURN BLOB
MEMBER PROCEDURE to_Clob(c IN OUT CLOB)
MEMBER PROCEDURE to_Blob(c IN OUT BLOB)
As noted in the documentation, "The serialization operation takes the in-memory representation of the JSON data and ...". Therefore if the parser converts the JSON to a NUMBER data type then only a NUMBER value will be stored and the formatting of the original JSON text is NOT stored. Since 10.00 is exactly the same value as 10 and a NUMBER does not store formatting then you can only retrieve the value 10 and not 10.00.
If you want to get the original text then you will have to use a different parser that lets you extract the underlying JSON text.
Alternatively, if all the numbers want to be formatted to 2 decimal places then you could use:
can_str := can_str || '"' || TO_CHAR(json_in.to_Number(), 'FM9999990D00') || '"';
fiddle
Did you try this?
ELSIF json_in.is_Scalar() THEN
IF json_in.is_String() THEN
can_str := can_str || json_in.to_String();
ELSIF json_in.is_Number() THEN
can_str := can_str ||'"'|| json_in.to_String()||'"';
END IF;
END IF;
Procedure:
`create or replace PROCEDURE ETA_JSON_SERIALIZE (
json_in IN JSON_ELEMENT_T,
can_str IN OUT VARCHAR2,
object_key IN VARCHAR2 DEFAULT NULL
)
IS
BEGIN
IF json_in.is_Object() THEN
DECLARE
l_object JSON_OBJECT_T := TREAT(json_in AS JSON_OBJECT_T);
l_keys JSON_KEY_LIST := l_object.get_Keys();
BEGIN
FOR i IN 1 .. l_keys.COUNT LOOP
can_str := can_str || '"'||UPPER(l_keys(i))||'"';
ETA_JSON_SERIALIZE(l_object.get(l_keys(i)), can_str,
l_keys(i));
END LOOP;
END;
ELSIF json_in.is_Array() THEN
DECLARE
l_array JSON_ARRAY_T := TREAT(json_in AS JSON_ARRAY_T);
BEGIN
FOR i IN 0 .. l_array.get_size - 1 LOOP
IF object_key IS NOT NULL THEN
can_str := can_str || '"'||UPPER(object_key)||'"';
END IF;
ETA_JSON_SERIALIZE(l_array.get(i), can_str);
END LOOP;
END;
ELSIF json_in.is_Scalar() THEN
IF json_in.is_String() THEN
can_str := can_str || json_in.to_String();
ELSIF json_in.is_Number() THEN
can_str := can_str ||'"'|| json_in.to_String()||'"';
END IF;
END IF;`
Output
"ISSUER""ADDRESS""BRANCHID""1""COUNTRY""EG""GOVERNATE""Cairo""REGIONCITY""Nasr City""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""113317713""NAME""Issuer Company""RECEIVER""ADDRESS""COUNTRY""EG""GOVERNATE""Egypt""REGIONCITY""Mufazat al Ismlyah""STREET""580 Clementina Key""BUILDINGNUMBER""Bldg. 0""POSTALCODE""68030""FLOOR""1""ROOM""123""LANDMARK""7660 Melody Trail""ADDITIONALINFORMATION""beside Townhall""TYPE""B""ID""313717919""NAME""Receiver""DOCUMENTTYPE""I""DOCUMENTTYPEVERSION""0.9""DATETIMEISSUED""2020-10-27T23:59:59Z""TAXPAYERACTIVITYCODE""4620""INTERNALID""IID1""PURCHASEORDERREFERENCE""P-233-A6375""PURCHASEORDERDESCRIPTION""purchase Order description""SALESORDERREFERENCE""1231""SALESORDERDESCRIPTION""Sales Order description""PROFORMAINVOICENUMBER""SomeValue""PAYMENT""BANKNAME""SomeValue""BANKADDRESS""SomeValue""BANKACCOUNTNO""SomeValue""BANKACCOUNTIBAN""""SWIFTCODE""""TERMS""SomeValue""DELIVERY""APPROACH""SomeValue""PACKAGING""SomeValue""DATEVALIDITY""2020-09-28T09:30:10Z""EXPORTPORT""SomeValue""COUNTRYOFORIGIN""EG""GROSSWEIGHT""10.50""NETWEIGHT""20.50""TERMS""SomeValue""INVOICELINES""INVOICELINES""DESCRIPTION""Computer1""ITEMTYPE""GPC""ITEMCODE""10001774""UNITTYPE""EA""QUANTITY""5""INTERNALCODE""IC0""SALESTOTAL""947.00""TOTAL""2969.89""VALUEDIFFERENCE""7.00""TOTALTAXABLEFEES""817.42""NETTOTAL""880.71""ITEMSDISCOUNT""5.00""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""189.40""AMOUNTSOLD""10.00""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""7""AMOUNT""66.29""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""272.07""SUBTYPE""T1""RATE""14.00""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""208.22""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30.00""SUBTYPE""T3""RATE""0.00""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""43.79""SUBTYPE""T4""RATE""5.00""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""123.30""SUBTYPE""T5""RATE""14.00""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60.00""SUBTYPE""T6""RATE""0.00""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""88.07""SUBTYPE""T7""RATE""10.00""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""123.30""SUBTYPE""T8""RATE""14.00""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""105.69""SUBTYPE""T9""RATE""12.00""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""88.07""SUBTYPE""T10""RATE""10.00""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""123.30""SUBTYPE""T11""RATE""14.00""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""105.69""SUBTYPE""T12""RATE""12.00""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""88.07""SUBTYPE""T13""RATE""10.00""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""123.30""SUBTYPE""T14""RATE""14.00""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""105.69""SUBTYPE""T15""RATE""12.00""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""88.07""SUBTYPE""T16""RATE""10.00""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""88.07""SUBTYPE""T17""RATE""10.00""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""123.30""SUBTYPE""T18""RATE""14.00""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""105.69""SUBTYPE""T19""RATE""12.00""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""88.07""SUBTYPE""T20""RATE""10.00""INVOICELINES""DESCRIPTION""Computer2""ITEMTYPE""GPC""ITEMCODE""10003752""UNITTYPE""EA""QUANTITY""7""INTERNALCODE""IC0""SALESTOTAL""662.90""TOTAL""2226.61""VALUEDIFFERENCE""6.00""TOTALTAXABLEFEES""621.51""NETTOTAL""652.90""ITEMSDISCOUNT""9.00""UNITVALUE""CURRENCYSOLD""EUR""AMOUNTEGP""94.70""AMOUNTSOLD""5.00""CURRENCYEXCHANGERATE""18.94""DISCOUNT""RATE""0""AMOUNT""10.00""TAXABLEITEMS""TAXABLEITEMS""TAXTYPE""T1""AMOUNT""205.47""SUBTYPE""T1""RATE""14.00""TAXABLEITEMS""TAXTYPE""T2""AMOUNT""157.25""SUBTYPE""T2""RATE""12""TAXABLEITEMS""TAXTYPE""T3""AMOUNT""30.00""SUBTYPE""T3""RATE""0.00""TAXABLEITEMS""TAXTYPE""T4""AMOUNT""32.20""SUBTYPE""T4""RATE""5.00""TAXABLEITEMS""TAXTYPE""T5""AMOUNT""91.41""SUBTYPE""T5""RATE""14.00""TAXABLEITEMS""TAXTYPE""T6""AMOUNT""60.00""SUBTYPE""T6""RATE""0.00""TAXABLEITEMS""TAXTYPE""T7""AMOUNT""65.29""SUBTYPE""T7""RATE""10.00""TAXABLEITEMS""TAXTYPE""T8""AMOUNT""91.41""SUBTYPE""T8""RATE""14.00""TAXABLEITEMS""TAXTYPE""T9""AMOUNT""78.35""SUBTYPE""T9""RATE""12.00""TAXABLEITEMS""TAXTYPE""T10""AMOUNT""65.29""SUBTYPE""T10""RATE""10.00""TAXABLEITEMS""TAXTYPE""T11""AMOUNT""91.41""SUBTYPE""T11""RATE""14.00""TAXABLEITEMS""TAXTYPE""T12""AMOUNT""78.35""SUBTYPE""T12""RATE""12.00""TAXABLEITEMS""TAXTYPE""T13""AMOUNT""65.29""SUBTYPE""T13""RATE""10.00""TAXABLEITEMS""TAXTYPE""T14""AMOUNT""91.41""SUBTYPE""T14""RATE""14.00""TAXABLEITEMS""TAXTYPE""T15""AMOUNT""78.35""SUBTYPE""T15""RATE""12.00""TAXABLEITEMS""TAXTYPE""T16""AMOUNT""65.29""SUBTYPE""T16""RATE""10.00""TAXABLEITEMS""TAXTYPE""T17""AMOUNT""65.29""SUBTYPE""T17""RATE""10.00""TAXABLEITEMS""TAXTYPE""T18""AMOUNT""91.41""SUBTYPE""T18""RATE""14.00""TAXABLEITEMS""TAXTYPE""T19""AMOUNT""78.35""SUBTYPE""T19""RATE""12.00""TAXABLEITEMS""TAXTYPE""T20""AMOUNT""65.29""SUBTYPE""T20""RATE""10.00""TOTALDISCOUNTAMOUNT""76.29""TOTALSALESAMOUNT""1609.90""NETAMOUNT""1533.61""TAXTOTALS""TAXTOTALS""TAXTYPE""T1""AMOUNT""477.54""TAXTOTALS""TAXTYPE""T2""AMOUNT""365.47""TAXTOTALS""TAXTYPE""T3""AMOUNT""60.00""TAXTOTALS""TAXTYPE""T4""AMOUNT""75.99""TAXTOTALS""TAXTYPE""T5""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T6""AMOUNT""120.00""TAXTOTALS""TAXTYPE""T7""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T8""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T9""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T10""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T11""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T12""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T13""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T14""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T15""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T16""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T17""AMOUNT""153.36""TAXTOTALS""TAXTYPE""T18""AMOUNT""214.71""TAXTOTALS""TAXTYPE""T19""AMOUNT""184.04""TAXTOTALS""TAXTYPE""T20""AMOUNT""153.36""TOTALAMOUNT""5191.50""EXTRADISCOUNTAMOUNT""5.00""TOTALITEMSDISCOUNTAMOUNT""14.00"

JSON won't validate

I'm trying to get this to validate, it's probably a comma or formatting but I can't work it out and it's driving me insane - It doesn't seem to fail initially not sure why it does later on.
For reference: https://umod.org/plugins/server-rewards#adding-an-item
Any help would be appreciated - Thanks in advance.
{
"items": {
"lmg.m249_0": {
"shortname": "lmg.m249",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "M249",
"cost": 100,
"cooldown": 0
},
"rifle.l96_0": {
"shortname": "rifle.l96",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "L96 Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.m39_0": {
"shortname": "rifle.m39",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "M39 Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.lr300_0": {
"shortname": "rifle.lr300",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "LR-300 Assault Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.ak_0": {
"shortname": "rifle.ak",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Assault Rifle",
"cost": 100,
"cooldown": 0
},
"pistol.python_0": {
"shortname": "pistol.python",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Python Revolver",
"cost": 100,
"cooldown": 0
},
"rocket.launcher_0": {
"shortname": "rocket.launcher",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Rocket Launcher",
"cost": 100,
"cooldown": 0
},
"multiplegrenadelauncher_0": {
"shortname": "multiplegrenadelauncher",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Multiple Grenade Launcher",
"cost": 100,
"cooldown": 0
},
"weapon.mod.lasersight_0": {
"shortname": "weapon.mod.lasersight",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Weapon Lasersight",
"cost": 100,
"cooldown": 0
},
"weapon.mod.silencer_0": {
"shortname": "weapon.mod.silencer",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Silencer",
"cost": 100,
"cooldown": 0
},
"ammo.rifle_0": {
"shortname": "ammo.rifle",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rifle.hv_0": {
"shortname": "ammo.rifle.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "HV 5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rifle.explosive_0": {
"shortname": "ammo.rifle.explosive",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Explosive 5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.shotgun_0: {
"shortname": "ammo.shotgun",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "12 Gauge Buckshot",
"cost": 100,
"cooldown": 0
},
"ammo.shotgun.slug_0": {
"shortname": "ammo.shotgun.slug",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "12 Gauge Slug",
"cost": 100,
"cooldown": 0
},
"ammo.pistol_0": {
"shortname": "ammo.pistol",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Pistol Bullet",
"cost": 100,
"cooldown": 0
},
"ammo.pistol.hv_0": {
"shortname": "ammo.pistol.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "HV Pistol Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.basic_0": {
"shortname": "ammo.rocket.basic",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Rocket",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.hv_0": {
"shortname": "ammo.rocket.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "High Velocity Rocket",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.sam_0": {
"shortname": "ammo.rocket.sam",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "SAM Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.grenadelauncher.he_0": {
"shortname": "ammo.grenadelauncher.he",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "40mm HE Grenade",
"cost": 100,
"cooldown": 0
},
"ammo.grenadelauncher.buckshot_0": {
"shortname": "ammo.grenadelauncher.buckshot",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "40mm Shotgun Round",
"cost": 100,
"cooldown": 0
}
},
"kits": {},
"commands": {}
}
The error was the extra } above "kits".
{
"items": {
"lmg.m249_0": {
"shortname": "lmg.m249",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "M249",
"cost": 100,
"cooldown": 0
},
"rifle.l96_0": {
"shortname": "rifle.l96",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "L96 Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.m39_0": {
"shortname": "rifle.m39",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "M39 Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.lr300_0": {
"shortname": "rifle.lr300",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "LR-300 Assault Rifle",
"cost": 100,
"cooldown": 0
},
"rifle.ak_0": {
"shortname": "rifle.ak",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Assault Rifle",
"cost": 100,
"cooldown": 0
},
"pistol.python_0": {
"shortname": "pistol.python",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Python Revolver",
"cost": 100,
"cooldown": 0
},
"rocket.launcher_0": {
"shortname": "rocket.launcher",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Rocket Launcher",
"cost": 100,
"cooldown": 0
},
"multiplegrenadelauncher_0": {
"shortname": "multiplegrenadelauncher",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Multiple Grenade Launcher",
"cost": 100,
"cooldown": 0
},
"weapon.mod.lasersight_0": {
"shortname": "weapon.mod.lasersight",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Weapon Lasersight",
"cost": 100,
"cooldown": 0
},
"weapon.mod.silencer_0": {
"shortname": "weapon.mod.silencer",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Silencer",
"cost": 100,
"cooldown": 0
},
"ammo.rifle_0": {
"shortname": "ammo.rifle",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rifle.hv_0": {
"shortname": "ammo.rifle.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "HV 5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rifle.explosive_0": {
"shortname": "ammo.rifle.explosive",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Explosive 5.56 Rifle Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.shotgun_0: {
"shortname": "ammo.shotgun",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "12 Gauge Buckshot",
"cost": 100,
"cooldown": 0
},
"ammo.shotgun.slug_0": {
"shortname": "ammo.shotgun.slug",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "12 Gauge Slug",
"cost": 100,
"cooldown": 0
},
"ammo.pistol_0": {
"shortname": "ammo.pistol",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Pistol Bullet",
"cost": 100,
"cooldown": 0
},
"ammo.pistol.hv_0": {
"shortname": "ammo.pistol.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "HV Pistol Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.basic_0": {
"shortname": "ammo.rocket.basic",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "Rocket",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.hv_0": {
"shortname": "ammo.rocket.hv",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "High Velocity Rocket",
"cost": 100,
"cooldown": 0
},
"ammo.rocket.sam_0": {
"shortname": "ammo.rocket.sam",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "SAM Ammo",
"cost": 100,
"cooldown": 0
},
"ammo.grenadelauncher.he_0": {
"shortname": "ammo.grenadelauncher.he",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "40mm HE Grenade",
"cost": 100,
"cooldown": 0
},
"ammo.grenadelauncher.buckshot_0": {
"shortname": "ammo.grenadelauncher.buckshot",
"customIcon": null,
"amount": 1,
"skinId": 0,
"isBp": false,
"category": 1,
"displayName": "40mm Shotgun Round",
"cost": 100,
"cooldown": 0
},
"kits": {},
"commands": {}
}

Multi Nested JSON in hive

I have the following input with nested JSON, I want to ingest this data into hive in multiple rows
"taxes": [{
"line_id": 1,
"commodity_code": "997159",
"fee": {
"amt": {
"curr_code": "USD",
"value": "71.4"
},
"type": "receiver"
},
"ship_addr": {
"admin_area_1": "MAHARASHTRA",
"country_code": "IN"
},
"total_tax": {
"curr_code": "USD",
"value": "12.8520000000"
},
"tax-details": [{
"exempt_option": false,
"auth_name": "India Maharashtra Central GST",
"doc_amt": {
"currency_code": "USD",
"value": "6.43"
},
"unrnd_doc_amt": {
"currency_code": "USD",
"value": "6.4260000000"
},
"rate": "0.09",
"rate_code": "SR",
"non_basis_doc_amt": "0.00",
"exempt_doc_amt": "0.00",
"jdx_memo": "INSS2: Tax payable in Seller location.",
"seller_reg_no": "27AAGCP4442G1ZF",
"admin_zone_level": "Country",
"auth_type": "CGST",
"erp_code": "MHCGST",
"inv_desc": "Standard Rate - CGST",
"basis_doc_amt": "71.40"
}, {
"exempt_option": false,
"auth_name": "India Maharashtra State GST",
"doc_amt": {
"currency_code": "USD",
"value": "6.43"
},
"unrnd_doc_amt": {
"currency_code": "USD",
"value": "6.4260000000"
},
"rate": "0.09",
"rate_code": "SR",
"non_basis_doc_amt": "0.00",
"exempt_doc_amt": "0.00",
"jdx_memo": "INSS2: Tax payable in Seller location.",
"seller_reg_no": "27AAGCP4442G1ZF",
"admin_zone_level": "Province",
"auth_type": "SGST",
"erp_code": "MHSGST",
"inv_desc": "Standard Rate - SGST",
"basis_doc_amt": "71.40"
}],
"transaction_type": "DS"
}]
I am using the following DDL
select
get_json_object(t.json,concat('$.taxes[',e.i,'].line_id')) as line_id
, get_json_object(t.json,concat('$.taxes[',e.i,'].commodity_code')) as commodity_code
, get_json_object(t.json,concat('$.taxes[',e.i,'].fee.amt.curr_code')) as curr_code
, get_json_object(t.json,concat('$.taxes[',e.i,'].fee.amt.value')) as value
, get_json_object(t.json,concat('$.taxes[',e.i,'].fee.type')) as type
, get_json_object(t.json,concat('$.taxes[',e.i,'].ship_addr.admin_area_1')) as admin_area
, get_json_object(t.json,concat('$.taxes[',e.i,'].ship_addr.country_code')) as country_code
, get_json_object(t.json,concat('$.taxes[',e.i,'].total_tax.curr_code')) as total_tax_curr_code
, get_json_object(t.json,concat('$.taxes[',e.i,'].total_tax.value')) as total_tax_value
get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].exempt_option')) as exempt_option
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].auth_name')) as auth_name
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].doc_amt.currency_code')) as doc_amt_currency_code
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].doc_amt.value')) as doc_amt_value
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].unrnd_doc_amt.currency_code')) as unrnd_doc_amt_currency_code
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].unrnd_doc_amt.value')) as unrnd_doc_amt_value
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].rate')) as rate
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].rate_code')) as rate_code
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].non_basis_doc_amt')) as non_basis_doc_amt
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].exempt_doc_amt')) as exempt_doc_amount
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].jdx_memo')) as jdx_memo
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].seller_reg_no')) as seller_reg_no
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].admin_zone_level')) as admin_zone_level
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].auth_type')) as auth_type
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].erp_code')) as erp_code
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].inv_desc')) as inv_desc
, get_json_object(t.json,concat('$.taxes.tax_details[',f.g,'].basis_doc_amt')) as basis_doc_amt
FROM json_2002 t
LATERAL VIEW POSEXPLODE (split(get_json_object(json,'$.taxes[*].line_id'),'","')) e as i,x
LATERAL VIEW POSEXPLODE (split(get_json_object(json,'$taxes.tax_details[*].exempt_option'),'","')) f as g,h
I was able to fix the issue by using regex_replace
LATERAL VIEW POSEXPLODE (split(regexp_replace(get_json_object(json,'$.taxes.tax_details[*].exempt_option'), '\[|\]', ''), ',' ) ) f as g,h
solved my problem

How Do I Sum the Total Of The Column Values If filtering the pending values in Angularjs?

I am using MEAN stack in my application with AngularJS as my front-end. How to total sum column values, if some value is filtered in the table, actually I got the total sum value but if I do filter like | filter:{status: 'pending'} in the table total sum gets overall data values...My Plunker For Example :- in table without filter the amt values total sum answer I got 5775.30, then in table if I have used filter like | filter:{status: 'pending'} amt values total sum is showing like overall value 5775.30, Expecting answer like 3850.2 , If any one knows the solution help to us thanks....
In table column without filter we got an exact answer and total sum also.
But in table, if used filter | filter:{status: 'pending'} again showing overall data values ... so what we expecting if we used filter the total sum should be calculate rest of the values ...expecting amt answer like 3850.2...please look at my plunker
My controller:-
.filter('sumOfValue', function () {
return function (data, key) {
debugger;
if (angular.isUndefined(data) && angular.isUndefined(key))
return 0;
var sum = 0;
angular.forEach(data,function(v,k){
var keyval;
if(isNaN(parseFloat(v[key])))
keyval=0;
else
keyval=parseFloat(v[key]);
sum = sum + keyval;
});
return sum.toFixed(2);
}
})
My Html:-
<td >{{mani.amt}}</td>
<td >{{mani.amount_payment }}</td>
<td >{{mani.status }}</td>
My filter:-
| filter:{status: 'pending'}
My Data:-
{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "100",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"status": "pending",
"buyer_name": "Manidesigns"
},
{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "100",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"status": "received",
"buyer_name": "Manidesigns"
},
{
"_id": "5816f4fad0be79f809519f98",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"created": "2016-10-31T07:38:34.999Z",
"remarks": "-",
"status": "pending",
"amt": "1925.10",
"cheque_currency": "Rs",
"cheque_value": "300",
"amount_payment": "",
"debitnote_no_payment": "3",
"supplier_name": "karikalan",
"status": "pending",
"buyer_name": "Manidesigns"
},
I have created with below given answer updated:- Plunker
You need to add the filter to the sum results.
<tr>
<td>sum</td>
<td>{{resultValue | filter:{status: 'pending'} | sumOfValue:'amt'}}</td>
<td>{{resultValue | filter:{status: 'pending'} | sumOfValue:'amount_payment'}}</td>
<td></td>
</tr>
Can you change your tr ng-repeat to below , you have not assigned filtered value to resultValue
<tr ng-repeat="mani in resultValue=((sryarndebitnote) | filter:{status: 'pending'}) ">