Groovy web console

subscribe to the feed Subscribe
to this
site
parse json and extract value (via #groovywebconsole)
tweet this snippet Tweet
this
script

parse json and extract value

Published 4 months ago by nmrao with tags josn parse
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def json = """{
   "products":[
      {
         "id":"2c92c0f85aff3453015b113678592b23",
         "sku":"SKU-00000218",
         "name":"Yealink W56P with Power Supply Purchase",
         "description":"Yealink W56P with PS Purchase. Wireless color screen DECT phone with Base station.",
         "InvoiceProductName__c":"Yealink W56P with Power Supply Purchase",
         "ProductFamily__c":"Phones",
         "SubscriptionType__c":"Hardware",
         "EffectiveEndDate":"2100-01-01",
         "productRatePlans":[
            {
               "id":"2c92c0f95aff3b53015b11367b692f1a",
               "name":"Yealink W56P with Power Supply Purchase",
               "description":"Test44498U53",
               "ProvisioningCode__c":"YealinkW56P",
               "CatalogThree__c":"",
               "Brand__c":"Yealink",
               "BillCode__c":"PH",
               "InvoiceRatePlanName__c":"Yealink W56P with Power Supply Purchase",
               "CatalogOne__c":"Direct,MasterAgent,SubAgent,GoldAgent,PremierAgent",
               "FloorValueRequiringSalesApproval__c":"112.49",
               "AccessLevel__c":"Care,Direct,Channel,Self-Add,Replace",
               "ExemptPeriodsRequiringSalesApproval__c":"",
               "FloorValueRequiringCareApproval__c":"106.24",
               "CatalogTwo__c":"",
               "MandatoryPlanonAcquire__c":"No",
               "AccountMinuteBucketBalance__c":"",
               "productRatePlanCharges":[
                  {
                     "id":"2c92c0f85aff33fe015b1136838042cc",
                     "name":"Yealink W56P with Power Supply Purchase",
                     "type":"OneTime",
                     "pricingSummary":[

                     ],
                     "BillSoft_TransactionType__c":"10",
                     "ChargeCode__c":"PH",
                     "E911_Applicable__c":"No",
                     "BillSoft_DiscountType__c":"No Discount",
                     "BillSoft_ServiceType__c":"15",
                     "productRatePlanChargeTiers":[
                        {
                           "id":"2c92c0f85aff33fe015b1136838142cd",
                           "active":true,
                           "currency":"USD",
                           "discountPercentage":0.0,
                           "endingUnit":0.0,
                           "includedUnits":0.0,
                           "price":124.99,
                           "priceFormat":"",
                           "startingUnit":0.0,
                           "tier":1
                        }
                     ],
                     "ChargeModel":"Per Unit Pricing",
                     "productRatePlanId":"2c92c0f95aff3b53015b11367b692f1a"
                  }
               ],
               "productId":"2c92c0f85aff3453015b113678592b23",
               "SKU__c":""
            },
            {
               "id":"2c92c0f95aff3b53015b11367b752f1c",
               "name":"Yealink W56P with Power Supply Purchase White Label",
               "description":"",
               "ProvisioningCode__c":"YealinkW56P",
               "CatalogThree__c":"",
               "Brand__c":"Yealink",
               "BillCode__c":"PH",
               "InvoiceRatePlanName__c":"Yealink W56P with Power Supply Purchase",
               "CatalogOne__c":"WS-Betterworld,WS-PreferredLongDistance,WSP-TruCom,WSP-Cornerstone,CB-RingSavvy,CB-Generic",
               "FloorValueRequiringSalesApproval__c":"0",
               "AccessLevel__c":"Care,Channel,Self-Add,Replace",
               "ExemptPeriodsRequiringSalesApproval__c":"",
               "FloorValueRequiringCareApproval__c":"0",
               "CatalogTwo__c":"",
               "MandatoryPlanonAcquire__c":"No",
               "AccountMinuteBucketBalance__c":"",
               "productRatePlanCharges":[
                  {
                     "id":"2c92c0f85aff33fe015b113683c742d0",
                     "name":"Yealink W56P with Power Supply Purchase",
                     "type":"OneTime",
                     "pricingSummary":[

                     ],
                     "BillSoft_TransactionType__c":"10",
                     "ChargeCode__c":"PH",
                     "E911_Applicable__c":"No",
                     "BillSoft_DiscountType__c":"No Discount",
                     "BillSoft_ServiceType__c":"15",
                     "productRatePlanChargeTiers":[
                        {
                           "id":"2c92c0f85aff33fe015b113683c842d1",
                           "active":true,
                           "currency":"USD",
                           "discountPercentage":0.0,
                           "endingUnit":0.0,
                           "includedUnits":0.0,
                           "price":0.0,
                           "priceFormat":"",
                           "startingUnit":0.0,
                           "tier":1
                        }
                     ],
                     "ChargeModel":"Per Unit Pricing",
                     "productRatePlanId":"2c92c0f95aff3b53015b11367b752f1c"
                  }
               ],
               "productId":"2c92c0f85aff3453015b113678592b23",
               "SKU__c":""
            }
         ]
      },
      {
         "id":"2c92c0f94db928f9014dcb82e4ae6cfe",
         "sku":"SKU-00000157",
         "name":"Account Discounts",
         "description":"",
         "InvoiceProductName__c":"Discount",
         "ProductFamily__c":"Discount",
         "SubscriptionType__c":"Service",
         "EffectiveEndDate":"2100-01-01",
         "productRatePlans":[
            {
               "id":"2c92c0f94db92903014dcb82f00b30ad",
               "name":"Percentage Discount",
               "description":"",
               "ProvisioningCode__c":"",
               "CatalogThree__c":"",
               "Brand__c":"",
               "BillCode__c":"Discount",
               "InvoiceRatePlanName__c":"Discount",
               "CatalogOne__c":"Direct,MasterAgent,SubAgent,GoldAgent,PremierAgent,CB-RingSavvy,CB-Generic",
               "FloorValueRequiringSalesApproval__c":"",
               "AccessLevel__c":"NoAccess",
               "ExemptPeriodsRequiringSalesApproval__c":"",
               "FloorValueRequiringCareApproval__c":"",
               "CatalogTwo__c":"",
               "MandatoryPlanonAcquire__c":"No",
               "AccountMinuteBucketBalance__c":"",
               "productRatePlanCharges":[
                  {
                     "id":"2c92c0f84db91fc9014dcb8310dc2916",
                     "name":"Percentage Discount",
                     "type":"Recurring",
                     "pricingSummary":[

                     ],
                     "BillSoft_TransactionType__c":"",
                     "ChargeCode__c":"MO",
                     "E911_Applicable__c":"No",
                     "BillSoft_DiscountType__c":"No Discount",
                     "BillSoft_ServiceType__c":"",
                     "productRatePlanChargeTiers":[
                        {
                           "id":"2c92c0f84db91fc9014dcb8310dd2917",
                           "active":true,
                           "currency":"USD",
                           "discountPercentage":0.0,
                           "endingUnit":0.0,
                           "includedUnits":0.0,
                           "priceFormat":"",
                           "startingUnit":0.0,
                           "tier":1
                        }
                     ],
                     "ChargeModel":"Discount-Percentage",
                     "productRatePlanId":"2c92c0f94db92903014dcb82f00b30ad"
                  }
               ],
               "productId":"2c92c0f94db928f9014dcb82e4ae6cfe",
               "SKU__c":""
            },
            {
               "id":"2c92c0f94db92903014dcb82f02730af",
               "name":"One-Time MRC Discount",
               "description":"",
               "ProvisioningCode__c":"",
               "CatalogThree__c":"",
               "Brand__c":"",
               "BillCode__c":"Discount",
               "InvoiceRatePlanName__c":"Discount",
               "CatalogOne__c":"Direct,MasterAgent,SubAgent,GoldAgent,PremierAgent,CB-RingSavvy,CB-Generic",
               "FloorValueRequiringSalesApproval__c":"",
               "AccessLevel__c":"NoAccess",
               "ExemptPeriodsRequiringSalesApproval__c":"",
               "FloorValueRequiringCareApproval__c":"",
               "CatalogTwo__c":"",
               "MandatoryPlanonAcquire__c":"No",
               "AccountMinuteBucketBalance__c":"",
               "productRatePlanCharges":[
                  {
                     "id":"2c92c0f84db91fc9014dcb831185291a",
                     "name":"Dollar Discount",
                     "type":"Recurring",
                     "pricingSummary":[

                     ],
                     "BillSoft_TransactionType__c":"",
                     "ChargeCode__c":"MO",
                     "E911_Applicable__c":"No",
                     "BillSoft_DiscountType__c":"No Discount",
                     "BillSoft_ServiceType__c":"",
                     "productRatePlanChargeTiers":[
                        {
                           "id":"2c92c0f84db91fc9014dcb8311852919",
                           "active":true,
                           "currency":"USD",
                           "discountAmount":0.0,
                           "discountPercentage":0.0,
                           "endingUnit":0.0,
                           "includedUnits":0.0,
                           "priceFormat":"",
                           "startingUnit":0.0,
                           "tier":1
                        }
                     ],
                     "ChargeModel":"Discount-Fixed Amount",
                     "productRatePlanId":"2c92c0f94db92903014dcb82f02730af"
                  }
               ],
               "productId":"2c92c0f94db928f9014dcb82e4ae6cfe",
               "SKU__c":""
            }
         ]
      }
   ]
}"""

def productPriceMap = [:]
def jsonParsed = new groovy.json.JsonSlurper().parseText(json)
jsonParsed.products.collect{ product ->
    product.productRatePlans.collect { ratePlan ->
       ratePlan.productRatePlanCharges.collect { charge ->
           charge.productRatePlanChargeTiers.collect{ tier ->
                if (tier.price) {
                   println "${tier.price} - ${product.name}"
                   productPriceMap[product.name] = tier.price
                  }
           }
       }
    }
}
print "Product and price map\n$productPriceMap"