Home » Java » replace all keys in a JSON object composed of JSONArray

replace all keys in a JSON object composed of JSONArray

Posted by: admin June 30, 2018 Leave a comment

Questions:

I have a JSONObject which is composed of JSONArrays against some keys.

Here is a sample JSONObject:

Input:

{"UserKey":"DLPAgent","Operation":"DLPRuleMatch","OrganizationId":"2f6cb1a6-ecb8-4578-b680-bf84ded07ff4","IncidentId":"2b2f39ad-84ec-4dce-5800-08d5bee87822","Workload":"OneDrive","SensitiveInfoDetectionIsIncluded":false,"RecordType":11,"Version":1,"UserId":"DLPAgent","CreationTime":"2018-05-21T06:46:18","SharePointMetaData":{"UniqueID":"5710471b-f370-4f0d-be43-f6ba83645137","SiteCollectionGuid":"1692891b-2a42-431e-befa-1da656ce5ec8","SiteCollectionUrl":"https://emumbapk-my.sharepoint.com/personal/qa_emumbapk_onmicrosoft_com","FileName":"Classified.docx","FilePathUrl":"https://emumbapk-my.sharepoint.com/personal/qa_emumbapk_onmicrosoft_com/Documents/Classified.docx","FileOwner":"[email protected]","From":"[email protected]","ItemCreationTime":"2018-04-24T11:40:09","ItemLastModifiedTime":"2018-04-24T11:40:23"},"PolicyDetails":[{"PolicyName":"My policy","Rules":[{"Actions":["NotifyUser"],"RuleId":"61e36207-5ad7-4bc2-94f0-7f8b207e142c","RuleMode":"Enable","ConditionsMatched":{"SensitiveInformation":[{"Confidence":75,"Count":1,"SensitiveType":"a2ce32a8-f935-4bb6-8e96-2a5157672e2c"},{"Confidence":85,"Count":1,"SensitiveType":"e55e2a32-f92d-4985-a35d-a0b269eb687b"},{"Confidence":94,"Count":1,"SensitiveType":"a44669fe-0d48-453d-a9b1-2cc83f2cba77"}]},"Severity":"Low","RuleName":"Low volume of content detected My policy"}],"PolicyId":"82111a23-de2c-418f-b052-67e1ef639100"}],"Id":"73734ce7-fb54-41ab-ec00-08d5bee68e61","UserType":4}

I want to replace all the keys with my own keys retaining the values.

For example I want to replace UserKey with user_key. How can I do this?

With simple JSONObject I was using:

JSONObject.put("my_key" , JSONObject.get("OriginalKey"));

But now I cant use JSONObject.get("OriginalKey") for JSONArray value against a key.

Answers: