0

I have a JSONObject in which keys are stored as jsonPath and I want to create a JSON.

{
  "officeName": "string",
  "bookingPolicies.spocCancelCutOff": 0,
  "bookingPolicies.remoteSignInAllowed": true,
  "bookingPolicies.wfhScheduleCutOff": 1,
  "bookingPolicies.editBookingEnabled": true,
  "bookingPolicies.wfhMaxDurationForBooking": 1,
  "bookingPolicies.expiryCutOffInMinutes": 15,
  "bookingPolicies.expiryNotificationCutOffInMinutes": 40,
  "bookingPolicies.wfhMinShiftDurationInMinute": 180,
  "bookingPolicies.wfhMaxShiftDurationInMinute": 720,
  "bookingPolicies.timeDiffShiftsMin": 1,
  "bookingPolicies.employeeEndTimeBookingCutoff": 2,
  "bookingPolicies.maxHoursAllowedForCheckin": 0,
  "bookingPolicies.gatepassDelaycutoff": 0,
  "bookingPolicies.numOfDays": 2,
  "officeUUID": "a7829e63-3a80-4fca-abf6-3bc3d48832ee",
  "bookingPolicies.wfhDisabled": true,
  "bookingPolicies.checkInFirstPremise": false,
  "bookingPolicies.maxShiftDuration": 0,
  "bookingPolicies.limitEmployeeBookingDays": true,
  "shiftSchedules.defineClockIn": true,
  "bookingPolicies.wfhCancelCutOff": 1,
  "bookingPolicies.onCallMinShiftDurationInMinute": 180,
  "bookingPolicies.wfhBookingAllowed.key1": "val2",
  "bookingPolicies.defLogoutDuration": 10,
  "bookingPolicies.limitEmployeeBookingDaysUnit": 4,
  "bookingPolicies.blockUserIfNotVaccinated": true,
  "bookingPolicies.showParking": true,
  "seatBooking.isAutoEntryAllowed": false,
  "bookingPolicies.wfhBookingAllowed.key4.key2[0]": "val2",
  "bookingPolicies.parkingSlotBufferTimeInMin": 1,
  "bookingPolicies.bookingPerDayEmp": 12,
  "bookingPolicies.defOnCallLogoutDurationInMinute": 540,
  "bookingPolicies.employeeCancelCutOff": 5,
  "bookingPolicies.minShiftDuration": 0,
  "bookingPolicies.employeeScheduleCutoff": 2,
  "bookingPolicies.defWfhLogoutDurationInMinute": 540,
  "bookingPolicies.wfhEditCutOff": 1,
  "bookingPolicies.onCallMaxShiftDurationInMinute": 720,
  "bookingPolicies.wfhMonthlyLimit": 8,
  "seatBooking.maxTimeBeforeClockin": false,
  "bookingPolicies.bookingEditCutOff": 10,
  "bookingPolicies.wfhClockInBuffer": 15,
  "bookingPolicies.teamCalendarEnabled": true,
  "bookingPolicies.seatBookingEnabled": true,
  "bookingPolicies.enableRecurrenceOnTeamPlanner": true,
  "bookingPolicies.wfhSpocSchecduleCutOff": 1,
  "bookingPolicies.wfhWeeklyLimit": 2,
  "bookingPolicies.employeeEndTimeScheduleCutoff": 0,
  "bookingPolicies.wfhSpocEditCutOff": 1,
  "bookingPolicies.minHoursAllowedForCheckin": 0,
  "bookingPolicies.maxBufferForCheckin": 3,
  "bookingPolicies.bookingLogoutEditCutOff": 0,
  "bookingPolicies.spocScheduleCutOff": 0,
  "bookingPolicies.wfhMinBetweenClockInClockOut": 0
}

I am using recursive approach but I am not able to create nested jsonObject and arrayValues. Any library suggestions would also be great. Or how to use JsonObjects to create nested json.

Harsh
  • 55
  • 1
  • 1
  • 9
  • Have you looked into the answers here: [Create JSON Object from various JSON Paths](https://stackoverflow.com/q/66560805/12567365) and also here: [How to add new node to Json using JsonPath](https://stackoverflow.com/q/51971642/12567365)? – andrewJames Aug 08 '22 at 20:34
  • yes checked, its not able to add new path. gives path exception. – Harsh Aug 09 '22 at 06:52

1 Answers1

2

Use library Josson function unflatten() with parameter '.[]' as the delimiter characters.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{\n" +
    "  \"officeName\": \"string\",\n" +
    "  \"bookingPolicies.spocCancelCutOff\": 0,\n" +
    "  \"bookingPolicies.remoteSignInAllowed\": true,\n" +
    "  \"bookingPolicies.wfhScheduleCutOff\": 1,\n" +
    "  \"bookingPolicies.editBookingEnabled\": true,\n" +
    "  \"bookingPolicies.wfhMaxDurationForBooking\": 1,\n" +
    "  \"bookingPolicies.expiryCutOffInMinutes\": 15,\n" +
    "  \"bookingPolicies.expiryNotificationCutOffInMinutes\": 40,\n" +
    "  \"bookingPolicies.wfhMinShiftDurationInMinute\": 180,\n" +
    "  \"bookingPolicies.wfhMaxShiftDurationInMinute\": 720,\n" +
    "  \"bookingPolicies.timeDiffShiftsMin\": 1,\n" +
    "  \"bookingPolicies.employeeEndTimeBookingCutoff\": 2,\n" +
    "  \"bookingPolicies.maxHoursAllowedForCheckin\": 0,\n" +
    "  \"bookingPolicies.gatepassDelaycutoff\": 0,\n" +
    "  \"bookingPolicies.numOfDays\": 2,\n" +
    "  \"officeUUID\": \"a7829e63-3a80-4fca-abf6-3bc3d48832ee\",\n" +
    "  \"bookingPolicies.wfhDisabled\": true,\n" +
    "  \"bookingPolicies.checkInFirstPremise\": false,\n" +
    "  \"bookingPolicies.maxShiftDuration\": 0,\n" +
    "  \"bookingPolicies.limitEmployeeBookingDays\": true,\n" +
    "  \"shiftSchedules.defineClockIn\": true,\n" +
    "  \"bookingPolicies.wfhCancelCutOff\": 1,\n" +
    "  \"bookingPolicies.onCallMinShiftDurationInMinute\": 180,\n" +
    "  \"bookingPolicies.wfhBookingAllowed.key1\": \"val2\",\n" +
    "  \"bookingPolicies.defLogoutDuration\": 10,\n" +
    "  \"bookingPolicies.limitEmployeeBookingDaysUnit\": 4,\n" +
    "  \"bookingPolicies.blockUserIfNotVaccinated\": true,\n" +
    "  \"bookingPolicies.showParking\": true,\n" +
    "  \"seatBooking.isAutoEntryAllowed\": false,\n" +
    "  \"bookingPolicies.wfhBookingAllowed.key4.key2[0]\": \"val2\",\n" +
    "  \"bookingPolicies.parkingSlotBufferTimeInMin\": 1,\n" +
    "  \"bookingPolicies.bookingPerDayEmp\": 12,\n" +
    "  \"bookingPolicies.defOnCallLogoutDurationInMinute\": 540,\n" +
    "  \"bookingPolicies.employeeCancelCutOff\": 5,\n" +
    "  \"bookingPolicies.minShiftDuration\": 0,\n" +
    "  \"bookingPolicies.employeeScheduleCutoff\": 2,\n" +
    "  \"bookingPolicies.defWfhLogoutDurationInMinute\": 540,\n" +
    "  \"bookingPolicies.wfhEditCutOff\": 1,\n" +
    "  \"bookingPolicies.onCallMaxShiftDurationInMinute\": 720,\n" +
    "  \"bookingPolicies.wfhMonthlyLimit\": 8,\n" +
    "  \"seatBooking.maxTimeBeforeClockin\": false,\n" +
    "  \"bookingPolicies.bookingEditCutOff\": 10,\n" +
    "  \"bookingPolicies.wfhClockInBuffer\": 15,\n" +
    "  \"bookingPolicies.teamCalendarEnabled\": true,\n" +
    "  \"bookingPolicies.seatBookingEnabled\": true,\n" +
    "  \"bookingPolicies.enableRecurrenceOnTeamPlanner\": true,\n" +
    "  \"bookingPolicies.wfhSpocSchecduleCutOff\": 1,\n" +
    "  \"bookingPolicies.wfhWeeklyLimit\": 2,\n" +
    "  \"bookingPolicies.employeeEndTimeScheduleCutoff\": 0,\n" +
    "  \"bookingPolicies.wfhSpocEditCutOff\": 1,\n" +
    "  \"bookingPolicies.minHoursAllowedForCheckin\": 0,\n" +
    "  \"bookingPolicies.maxBufferForCheckin\": 3,\n" +
    "  \"bookingPolicies.bookingLogoutEditCutOff\": 0,\n" +
    "  \"bookingPolicies.spocScheduleCutOff\": 0,\n" +
    "  \"bookingPolicies.wfhMinBetweenClockInClockOut\": 0\n" +
    "}");

Transformation

JsonNode node = josson.getNode("unflatten('.[]')");
System.out.println(node.toPrettyString());

Output

{
  "officeName" : "string",
  "bookingPolicies" : {
    "spocCancelCutOff" : 0,
    "remoteSignInAllowed" : true,
    "wfhScheduleCutOff" : 1,
    "editBookingEnabled" : true,
    "wfhMaxDurationForBooking" : 1,
    "expiryCutOffInMinutes" : 15,
    "expiryNotificationCutOffInMinutes" : 40,
    "wfhMinShiftDurationInMinute" : 180,
    "wfhMaxShiftDurationInMinute" : 720,
    "timeDiffShiftsMin" : 1,
    "employeeEndTimeBookingCutoff" : 2,
    "maxHoursAllowedForCheckin" : 0,
    "gatepassDelaycutoff" : 0,
    "numOfDays" : 2,
    "wfhDisabled" : true,
    "checkInFirstPremise" : false,
    "maxShiftDuration" : 0,
    "limitEmployeeBookingDays" : true,
    "wfhCancelCutOff" : 1,
    "onCallMinShiftDurationInMinute" : 180,
    "wfhBookingAllowed" : {
      "key1" : "val2",
      "key4" : {
        "key2" : [ "val2" ]
      }
    },
    "defLogoutDuration" : 10,
    "limitEmployeeBookingDaysUnit" : 4,
    "blockUserIfNotVaccinated" : true,
    "showParking" : true,
    "parkingSlotBufferTimeInMin" : 1,
    "bookingPerDayEmp" : 12,
    "defOnCallLogoutDurationInMinute" : 540,
    "employeeCancelCutOff" : 5,
    "minShiftDuration" : 0,
    "employeeScheduleCutoff" : 2,
    "defWfhLogoutDurationInMinute" : 540,
    "wfhEditCutOff" : 1,
    "onCallMaxShiftDurationInMinute" : 720,
    "wfhMonthlyLimit" : 8,
    "bookingEditCutOff" : 10,
    "wfhClockInBuffer" : 15,
    "teamCalendarEnabled" : true,
    "seatBookingEnabled" : true,
    "enableRecurrenceOnTeamPlanner" : true,
    "wfhSpocSchecduleCutOff" : 1,
    "wfhWeeklyLimit" : 2,
    "employeeEndTimeScheduleCutoff" : 0,
    "wfhSpocEditCutOff" : 1,
    "minHoursAllowedForCheckin" : 0,
    "maxBufferForCheckin" : 3,
    "bookingLogoutEditCutOff" : 0,
    "spocScheduleCutOff" : 0,
    "wfhMinBetweenClockInClockOut" : 0
  },
  "officeUUID" : "a7829e63-3a80-4fca-abf6-3bc3d48832ee",
  "shiftSchedules" : {
    "defineClockIn" : true
  },
  "seatBooking" : {
    "isAutoEntryAllowed" : false,
    "maxTimeBeforeClockin" : false
  }
}
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8