Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help tackling json tree parent and child lookup #3006

Open
rexkani opened this issue Jul 30, 2021 · 7 comments
Open

Need help tackling json tree parent and child lookup #3006

rexkani opened this issue Jul 30, 2021 · 7 comments

Comments

@rexkani
Copy link

rexkani commented Jul 30, 2021

Hi all, i have a scenario where i can poll the API to get the booking status of some tennis courts. and i've been trying to look for some free slots and notify myself.

i want to look for "isFree": true, corresponding time; and also get the corresponding parent element "nameEn": "3/F Tennis Court - Main Court A".

i have been playing around with json path syntax, and no matter what i do, even filter by child to get parent object, i cannot achieve my goal because of UNEVEN objects. the only thing that i can think of now is maybe i have to flatten the JSON tree? is there anyway to do that by any agent?

filter by child:

{
  "expected_update_period_in_days": "8",
  "type": "json",
  "mode": "on_change",
  "data_from_event": "{{data | json}}",
  "extract": {
    "court": {
      "path": "..facilityBookingDailyTimeSlotDtoList[?(@.timeslotInfoList[?(@.isFree == \"false\")])].nameEn"
    },
    "startDateTime": {
      "path": "..facilityBookingDailyTimeSlotDtoList[?(@.timeslotInfoList[?(@.isFree == \"false\")])].timeslotInfoList[*].startDateTime"
    },
    "isFree": {
      "path": "..facilityBookingDailyTimeSlotDtoList[?(@.timeslotInfoList[?(@.isFree == \"false\")])].timeslotInfoList[*].isFree"
    }
  }
}

JSON data sample:

[
	{
		"displayDate": "2021-07-30",
		"facilityBookingDailyTimeSlotDtoList": [
			{
				"facilityId": 1,
				"nameEn": "3/F Tennis Court - Main Court A",
				"nameSc": "3/F網球场 正场 A",
				"nameTc": "3/F網球場 正場 A",
				"gridDisplayName": "3A",
				"timeslotInfoList": [
					{
						"fee": 80,
						"startDateTime": "2021-07-30 08:00:00",
						"endDateTime": "2021-07-30 09:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 09:00:00",
						"endDateTime": "2021-07-30 10:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 10:00:00",
						"endDateTime": "2021-07-30 11:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 11:00:00",
						"endDateTime": "2021-07-30 12:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 12:00:00",
						"endDateTime": "2021-07-30 13:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 13:00:00",
						"endDateTime": "2021-07-30 14:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 14:00:00",
						"endDateTime": "2021-07-30 15:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 15:00:00",
						"endDateTime": "2021-07-30 16:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 16:00:00",
						"endDateTime": "2021-07-30 17:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 17:00:00",
						"endDateTime": "2021-07-30 18:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 18:00:00",
						"endDateTime": "2021-07-30 19:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 19:00:00",
						"endDateTime": "2021-07-30 20:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 20:00:00",
						"endDateTime": "2021-07-30 21:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 21:00:00",
						"endDateTime": "2021-07-30 22:00:00",
						"bookingInfoList": [],
						"isFree": false
					}
				]
			},
			{
				"facilityId": 2,
				"nameEn": "3/F Tennis Court - Main Court B",
				"nameSc": "3/F網球场 正场 B",
				"nameTc": "3/F網球場 正場 B",
				"gridDisplayName": "3B",
				"timeslotInfoList": [
					{
						"fee": 80,
						"startDateTime": "2021-07-30 08:00:00",
						"endDateTime": "2021-07-30 09:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 09:00:00",
						"endDateTime": "2021-07-30 10:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 10:00:00",
						"endDateTime": "2021-07-30 11:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 11:00:00",
						"endDateTime": "2021-07-30 12:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 12:00:00",
						"endDateTime": "2021-07-30 13:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 13:00:00",
						"endDateTime": "2021-07-30 14:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 14:00:00",
						"endDateTime": "2021-07-30 15:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 15:00:00",
						"endDateTime": "2021-07-30 16:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 16:00:00",
						"endDateTime": "2021-07-30 17:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 17:00:00",
						"endDateTime": "2021-07-30 18:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 18:00:00",
						"endDateTime": "2021-07-30 19:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 19:00:00",
						"endDateTime": "2021-07-30 20:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 20:00:00",
						"endDateTime": "2021-07-30 21:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 21:00:00",
						"endDateTime": "2021-07-30 22:00:00",
						"bookingInfoList": [],
						"isFree": false
					}
				]
			},
			{
				"facilityId": 3,
				"nameEn": "3/F Tennis Court - Main Court C",
				"nameSc": "3/F網球场 正场 C",
				"nameTc": "3/F網球場 正場 C",
				"gridDisplayName": "3C",
				"timeslotInfoList": [
					{
						"fee": 80,
						"startDateTime": "2021-07-30 08:00:00",
						"endDateTime": "2021-07-30 09:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 09:00:00",
						"endDateTime": "2021-07-30 10:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 10:00:00",
						"endDateTime": "2021-07-30 11:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 11:00:00",
						"endDateTime": "2021-07-30 12:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 12:00:00",
						"endDateTime": "2021-07-30 13:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 13:00:00",
						"endDateTime": "2021-07-30 14:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 14:00:00",
						"endDateTime": "2021-07-30 15:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 15:00:00",
						"endDateTime": "2021-07-30 16:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 16:00:00",
						"endDateTime": "2021-07-30 17:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 17:00:00",
						"endDateTime": "2021-07-30 18:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 18:00:00",
						"endDateTime": "2021-07-30 19:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 19:00:00",
						"endDateTime": "2021-07-30 20:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 20:00:00",
						"endDateTime": "2021-07-30 21:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 21:00:00",
						"endDateTime": "2021-07-30 22:00:00",
						"bookingInfoList": [],
						"isFree": false
					}
				]
			},
			{
				"facilityId": 4,
				"nameEn": "3/F Tennis Court - Main Court D",
				"nameSc": "3/F網球场 正场 D",
				"nameTc": "3/F網球場 正場 D",
				"gridDisplayName": "3D",
				"timeslotInfoList": [
					{
						"fee": 80,
						"startDateTime": "2021-07-30 08:00:00",
						"endDateTime": "2021-07-30 09:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 09:00:00",
						"endDateTime": "2021-07-30 10:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 10:00:00",
						"endDateTime": "2021-07-30 11:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 11:00:00",
						"endDateTime": "2021-07-30 12:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 12:00:00",
						"endDateTime": "2021-07-30 13:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 13:00:00",
						"endDateTime": "2021-07-30 14:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 14:00:00",
						"endDateTime": "2021-07-30 15:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 15:00:00",
						"endDateTime": "2021-07-30 16:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 16:00:00",
						"endDateTime": "2021-07-30 17:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 80,
						"startDateTime": "2021-07-30 17:00:00",
						"endDateTime": "2021-07-30 18:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 18:00:00",
						"endDateTime": "2021-07-30 19:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 19:00:00",
						"endDateTime": "2021-07-30 20:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 20:00:00",
						"endDateTime": "2021-07-30 21:00:00",
						"bookingInfoList": [],
						"isFree": false
					},
					{
						"fee": 95,
						"startDateTime": "2021-07-30 21:00:00",
						"endDateTime": "2021-07-30 22:00:00",
						"bookingInfoList": [],
						"isFree": false
					}
				]
			}
		]
	}
]
@Unending
Copy link
Collaborator

You'll keep running into uneven errors this way. What you can do is put two website agents in series and use the "repeat": "true" option on the second one.

  • First one "path": "..facilityBookingDailyTimeSlotDtoList.[*]" creates an event per facility.
  • And the second one creates an event per timeslot
   "court": {
     "path": "nameEn",
     "repeat": "true"
   },
   "startDateTime": {
     "path": "timeslotInfoList[*].startDateTime"
   },
   "isFree": {
     "path": "timeslotInfoList[*].isFree"
   }

Additionally, I would suggest using a trigger agent to filter out the "isFree": false rather than doing it directly in jsonpath.

@rexkani
Copy link
Author

rexkani commented Jul 31, 2021 via email

@Unending
Copy link
Collaborator

I mean a third agent. As for why, I've often ran into weird or maintainability issues when trying to do to many things at once in a single agent. But whatever works for you.

@virtadpt
Copy link
Collaborator

virtadpt commented Aug 1, 2021

@Unending Could you please post the full code for the second Website Agent? I was playing around with this yesterday (I have a similar problem right now) but never got it to work the way it was supposed to.

@Unending
Copy link
Collaborator

Unending commented Aug 1, 2021

@virtadpt not much more to it:

{
  "expected_update_period_in_days": "2",
  "type": "json",
  "mode": "on_change",
  "data_from_event": "{{ data | json }}",
  "extract": {
    "court": {
      "path": "nameEn",
      "repeat": "true"
    },
    "startDateTime": {
      "path": "timeslotInfoList[*].startDateTime"
    },
    "isFree": {
      "path": "timeslotInfoList[*].isFree"
    }
  }
}

@rexkani
Copy link
Author

rexkani commented Aug 2, 2021

@Unending 's solution worked for me and a third trigger agent is definitely needed. because if we try to filter "isFree" by true/false, the number of items of "isFree" must be less than the number of "startDateTime" because "startDateTime" will list all slots no matter free or not.

thank you very much for your help!

@virtadpt
Copy link
Collaborator

virtadpt commented Aug 2, 2021

"data_from_event": "{{ data | json }}",

That's what I did wrong - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants