I've been trying to get this using XPath but struggling with the same as you - converting each of the values returned from the XPath expression to JSON. I'm not even sure if it's possible given we don't have an each we can use within the items returned.
Just a quick question - how many items are you working with? If not a massive amount, then you could still do the following using just JSON which would be fairly efficient.
See full flow below. I'll go into each of the actions.

For this example, I've used the following data for the items.
[
{
"id": "1",
"title": "Item 001"
},
{
"id": "2",
"title": "Item 002"
},
{
"id": "3",
"title": "Item 003"
}
]

And for the comments:
{
"comments": [
{
"id": "1",
"projectId": "1",
"comment": "This is the first comment for Item 001"
},
{
"id": "2",
"projectId": "1",
"comment": "This is the second comment for Item 001"
},
{
"id": "3",
"projectId": "2",
"comment": "This is the first comment for Item 002"
},
{
"id": "4",
"projectId": "3",
"comment": "This is the first comment for Item 003"
}
]
}

Apply to each iterates over each of the items in the Items.

Filter array takes in the items in the Comments array. This will return all the comments associated with the current item.
outputs('Comments')?['comments']
And the Condition:
item()?['projectId']
items('Apply_to_each')?['id']

Compose will add a new property using the following expression:
addProperty(items('Apply_to_each'), 'comments', body('Filter_array'))

Data will then take the output from the Compose that should contain all the data you want.

The final output from Data would look like that below.
[
{
"id": "1",
"title": "Item 001",
"comments": [
{
"id": "1",
"projectId": "1",
"comment": "This is the first comment for Item 001"
},
{
"id": "2",
"projectId": "1",
"comment": "This is the second comment for Item 001"
}
]
},
{
"id": "2",
"title": "Item 002",
"comments": [
{
"id": "3",
"projectId": "2",
"comment": "This is the first comment for Item 002"
}
]
},
{
"id": "3",
"title": "Item 003",
"comments": [
{
"id": "4",
"projectId": "3",
"comment": "This is the first comment for Item 003"
}
]
}
]
This is fairly efficient but depends on how many projects you are iterating over.