Ad

JSON Iteration In JAVA Using Jettison Library

- 1 answer

I have this complex JSON data coming from SalesForce to my Java Spring Application. Now I want to get some specific values like ID, Fund_Ops_Code__c, Type, Product_Type__c, Signed_Date__c, Signed_State__c and so on. But there is no pattern in JSON. I HAVE to use Jettison library in Java. Can someone please help me to iterate these values?

{
"totalSize": 4,
"done": true,
"records": [
  {
   "attributes": {
    "type": "COB_Plan_Setup__c",
    "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000001GgkKEAS"
   },
  "Id": "a1Xc0000001GgkKEAS",
   "Quote_ID__c": "0Q0c0000000Yb0pCAC",
   "Quote_ID__r": {
    "attributes": {
     "type": "Quote",
     "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000Yb0pCAC"
    },
    "Fund_Product__r": {
     "attributes": {
      "type": "Product2",
      "url": "\/services\/data\/v32.0\/sobjects\/Product2\/01tc00000045sOcAAI"
     },
     "Fund_Ops_Code__c": "MK4000"
    },
    "Fund_Menu__c": "Menu 2",
    "Managed_Accounts_Custom__c": false,
    "Opportunity": {
     "attributes": {
      "type": "Opportunity",
      "url": "\/services\/data\/v32.0\/sobjects\/Opportunity\/006c000000FZdlAAAT"
     },
     "Type": "New Business"
    },
    "Final__c": null,
    "Fund_Margin_Code__c": null,
    "Product_Type__c": "MAP Select Allocated"
   },
   "MSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_MSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_MSA__c\/a1Sc0000002dtbGEAQ"
     },
     "Id": "a1Sc0000002dtbGEAQ",
     "Product__c": "Voya Map Select",
     "Signed_Date__c": "2017-05-04",
     "Signed_State__c": "KS"
    }]
   },
   "Plan_Digests__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_Plan_Digest__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Digest__c\/a1gc00000041VTTAA2"
     },
     "Id": "a1gc00000041VTTAA2",
     "Plan_Number__c": null,
     "Company_Name__c": "Aerosol Gas Company, Inc.",
     "Plan_Name__c": null,
     "Permit_Participants_age_50__c": null,
     "Match_Formula__c": null,
     "Translate_MF__c": null
    }]
   },
   "PSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_PSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_PSA__c\/a1Tc000001Xxua1EAB"
     },
     "Id": "a1Tc000001Xxua1EAB",
     "Participant_Online_Beneficiary_Storage__c": false,
     "QDIA__c": false,
     "Apply_Voya_Automatic_Enrollment_Service__c": false,
     "Participant_Eligibility_Tracking__c": false,
     "Voya_enroll_pre_poluate_match__c": true,
     "Default_Investment_Option__c": null,
     "Notification_Service__c": false
    }]
   },
   "Other_Agreements__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "Other_Agreement__c",
      "url": "\/services\/data\/v32.0\/sobjects\/Other_Agreement__c\/a1hc0000001D2rBAAS"
     },
     "Id": "a1hc0000001D2rBAAS",
     "Plan_Setup__r": {
      "attributes": {
       "type": "COB_Plan_Setup__c",
       "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000001GgkKEAS"
      },
      "Quote_ID__r": {
       "attributes": {
        "type": "Quote",
        "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000Yb0pCAC"
       },
       "Managed_Accounts_Custom__c": false
      }
     },
     "Voya_Fixed_Account_OA__c": null,
     "Voya_Administrative_Record_Keeping_Fee_O__c": null,
     "Basis_Points_Payable_To_Morningstar_OA__c": null,
     "Voya_Fixed_Account_OA__r": null,
     "Allocation_OA__c": 100
    }]
   }
  },
  {
   "attributes": {
    "type": "COB_Plan_Setup__c",
    "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000000SbC4EAK"
   },
   "Id": "a1Xc0000000SbC4EAK",
   "Quote_ID__c": "0Q0c0000000KrLvCAK",
   "Quote_ID__r": {
    "attributes": {
     "type": "Quote",
     "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000KrLvCAK"
    },
    "Fund_Product__r": {
     "attributes": {
      "type": "Product2",
      "url": "\/services\/data\/v32.0\/sobjects\/Product2\/01tc00000045sPYAAY"
     },
     "Fund_Ops_Code__c": "MLH000"
    },
    "Fund_Menu__c": "Menu 5",
    "Managed_Accounts_Custom__c": false,
    "Opportunity": {
     "attributes": {
      "type": "Opportunity",
      "url": "\/services\/data\/v32.0\/sobjects\/Opportunity\/006c000000HGQneAAH"
     },
     "Type": "New Business"
    },
    "Final__c": null,
    "Fund_Margin_Code__c": "900",
    "Product_Type__c": "MAP Select Allocated"
   },
   "MSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_MSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_MSA__c\/a1Sc0000002abnfEAA"
     },
     "Id": "a1Sc0000002abnfEAA",
     "Product__c": "Voya Map Select",
     "Signed_Date__c": "2017-11-01",
     "Signed_State__c": "CT"
    }]
   },
   "Plan_Digests__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_Plan_Digest__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Digest__c\/a1gc0000004KIWcAAO"
     },
     "Id": "a1gc0000004KIWcAAO",
     "Plan_Number__c": "819999",
     "Company_Name__c": "FISH 5",
     "Plan_Name__c": "Fish 5",
     "Permit_Participants_age_50__c": "Yes",
     "Match_Formula__c": "Employer matches 100% up to 3%",
     "Translate_MF__c": "100-3"
    }]
   },
   "PSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_PSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_PSA__c\/a1Tc000001YBhkvEAD"
     },
     "Id": "a1Tc000001YBhkvEAD",
     "Participant_Online_Beneficiary_Storage__c": true,
     "QDIA__c": true,
     "Apply_Voya_Automatic_Enrollment_Service__c": false,
     "Participant_Eligibility_Tracking__c": false,
     "Voya_enroll_pre_poluate_match__c": false,
     "Default_Investment_Option__c": "Target Date",
     "Notification_Service__c": true
    }]
   },
   "Other_Agreements__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "Other_Agreement__c",
      "url": "\/services\/data\/v32.0\/sobjects\/Other_Agreement__c\/a1hc0000000ibQEAAY"
     },
     "Id": "a1hc0000000ibQEAAY",
     "Plan_Setup__r": {
      "attributes": {
       "type": "COB_Plan_Setup__c",
       "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000000SbC4EAK"
      },
      "Quote_ID__r": {
       "attributes": {
        "type": "Quote",
        "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000KrLvCAK"
       },
       "Managed_Accounts_Custom__c": false
      }
     },
     "Voya_Fixed_Account_OA__c": null,
     "Voya_Administrative_Record_Keeping_Fee_O__c": null,
     "Basis_Points_Payable_To_Morningstar_OA__c": null,
     "Voya_Fixed_Account_OA__r": null,
     "Allocation_OA__c": 100
    }]
   }
  },
  {
   "attributes": {
    "type": "COB_Plan_Setup__c",
   "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000001GgkFEAS"
   },
   "Id": "a1Xc0000001GgkFEAS",
   "Quote_ID__c": "0Q0c0000000bmAvCAI",
   "Quote_ID__r": {
    "attributes": {
     "type": "Quote",
     "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000bmAvCAI"
    },
    "Fund_Product__r": null,
    "Fund_Menu__c": null,
    "Managed_Accounts_Custom__c": false,
    "Opportunity": {
     "attributes": {
      "type": "Opportunity",
      "url": "\/services\/data\/v32.0\/sobjects\/Opportunity\/006c000000FTLE3AAP"
     },
     "Type": "New Business"
    },
    "Final__c": null,
    "Fund_Margin_Code__c": null,
    "Product_Type__c": "Framewor(k) 2012"
   },
   "MSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_MSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_MSA__c\/a1Sc0000002dta2EAA"
     },
     "Id": "a1Sc0000002dta2EAA",
     "Product__c": "Voya FrameWork",
     "Signed_Date__c": null,
    "Signed_State__c": null
    }]
   },
   "Plan_Digests__r": null,
   "PSAs__r": null,
   "Other_Agreements__r": null
  },
  {
   "attributes": {
    "type": "COB_Plan_Setup__c",
    "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000001l5zEEAQ"
   },
   "Id": "a1Xc0000001l5zEEAQ",
   "Quote_ID__c": "0Q0c0000000ghcNCAQ",
   "Quote_ID__r": {
    "attributes": {
     "type": "Quote",
     "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000ghcNCAQ"
    },
    "Fund_Product__r": {
     "attributes": {
      "type": "Product2",
      "url": "\/services\/data\/v32.0\/sobjects\/Product2\/01tc00000045sQEAAY"
     },
     "Fund_Ops_Code__c": "MK1000"
    },
    "Fund_Menu__c": null,
    "Managed_Accounts_Custom__c": false,
    "Opportunity": {
     "attributes": {
      "type": "Opportunity",
      "url": "\/services\/data\/v32.0\/sobjects\/Opportunity\/006c000000HguLjAAJ"
     },
     "Type": "Existing Business"
    },
    "Final__c": null,
    "Fund_Margin_Code__c": null,
    "Product_Type__c": "MAP Select Allocated"
   },
   "MSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_MSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_MSA__c\/a1Sc0000002ayKAEAY"
     },
     "Id": "a1Sc0000002ayKAEAY",
     "Product__c": "Voya Map Select",
     "Signed_Date__c": null,
     "Signed_State__c": null
    }]
   },
   "Plan_Digests__r": {
   "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_Plan_Digest__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Digest__c\/a1gc0000004KWnVAAW"
     },
     "Id": "a1gc0000004KWnVAAW",
     "Plan_Number__c": "775319",
     "Company_Name__c": "New Oppty_1",
     "Plan_Name__c": "TestPlan2018",
     "Permit_Participants_age_50__c": null,
     "Match_Formula__c": null,
     "Translate_MF__c": null
    }]
   },
   "PSAs__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "COB_PSA__c",
      "url": "\/services\/data\/v32.0\/sobjects\/COB_PSA__c\/a1Tc000001YCmv1EAD"
     },
     "Id": "a1Tc000001YCmv1EAD",
     "Participant_Online_Beneficiary_Storage__c": false,
     "QDIA__c": false,
     "Apply_Voya_Automatic_Enrollment_Service__c": false,
     "Participant_Eligibility_Tracking__c": false,
     "Voya_enroll_pre_poluate_match__c": true,
     "Default_Investment_Option__c": null,
     "Notification_Service__c": false
    }]
   },
   "Other_Agreements__r": {
    "totalSize": 1,
    "done": true,
    "records": [{
     "attributes": {
      "type": "Other_Agreement__c",
      "url": "\/services\/data\/v32.0\/sobjects\/Other_Agreement__c\/a1hc0000001uKjlAAE"
    },
     "Id": "a1hc0000001uKjlAAE",
     "Plan_Setup__r": {
      "attributes": {
       "type": "COB_Plan_Setup__c",
       "url": "\/services\/data\/v32.0\/sobjects\/COB_Plan_Setup__c\/a1Xc0000001l5zEEAQ"
      },
      "Quote_ID__r": {
       "attributes": {
        "type": "Quote",
        "url": "\/services\/data\/v32.0\/sobjects\/Quote\/0Q0c0000000ghcNCAQ"
       },
       "Managed_Accounts_Custom__c": false
      }
     },
     "Voya_Fixed_Account_OA__c": null,
     "Voya_Administrative_Record_Keeping_Fee_O__c": null,
     "Basis_Points_Payable_To_Morningstar_OA__c": null,
     "Voya_Fixed_Account_OA__r": null,
     "Allocation_OA__c": 100
    }]
   }
  }
]
}
Ad

Answer

You can use java JSON-Path library. it has a robust query language to search and retrieve specific JSON objects and properties

    try (InputStream is = Files.newInputStream(Paths.get("C://temp/test.json"))) {
        // get all 'Product_Type__c' properties wherever they are
        JSONArray productTypes = JsonPath.read(is, "$..Product_Type__c");
        System.out.println(productTypes.get(0));
    } catch (Exception e) {
        e.printStackTrace();
    }

Output:

MAP Select Allocated
Ad
source: stackoverflow.com
Ad