# 💫🪗 JSON Trebuchet

**URL:** <https://community.glideapps.com/t/json-trebuchet/72744>\
**Category:** Community Resources\
**Tags:** tutorial\
**Created:** [April 19, 2024, 10:57pm UTC](https://community.glideapps.com/t/json-trebuchet/72744 "2024-04-19T22:57:20Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [May 6, 2025, 12:11am UTC](https://community.glideapps.com/t/json-trebuchet/72744/21 "2025-05-06T00:11:52Z")

</div>

Sample JSON:

```auto
{
  "orderId": "ORD-12345",
  "customerId": "CUST-ABC",
  "timestamp": "2023-10-27T10:30:00Z",
  "items": [
    {
      "lineItemId": "LI-001",
      "productId": "PROD-A1",
      "productName": "Premium Widget",
      "quantity": 2,
      "pricePerItem": 19.99,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    },
    {
      "lineItemId": "LI-002",
      "productId": "PROD-B1",
      "productName": "Standard Gadget",
      "quantity": 5,
      "pricePerItem": 5.50,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    },
    {
      "lineItemId": "LI-003",
      "productId": "PROD-A2",
      "productName": "Deluxe Widget",
      "quantity": 1,
      "pricePerItem": 45.00,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    },
    {
      "lineItemId": "LI-004",
      "productId": "PROD-C1",
      "productName": "Basic Thingamajig",
      "quantity": 10,
      "pricePerItem": 1.25,
      "distributor": {
        "id": "DIST-03",
        "name": "Things R Us",
        "email": "orders@thingsrus.net"
      }
    },
    {
      "lineItemId": "LI-005",
      "productId": "PROD-B2",
      "productName": "Advanced Gadget",
      "quantity": 3,
      "pricePerItem": 15.00,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    }
  ]
}

```

JSONata query you can use to group by distributor ID:

```auto
items {
  distributor.id: $append($[$.distributor.id = distributor.id], $)
}

```

Sample result:

```auto
{
  "DIST-01": [
    {
      "lineItemId": "LI-001",
      "productId": "PROD-A1",
      "productName": "Premium Widget",
      "quantity": 2,
      "pricePerItem": 19.99,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    },
    {
      "lineItemId": "LI-003",
      "productId": "PROD-A2",
      "productName": "Deluxe Widget",
      "quantity": 1,
      "pricePerItem": 45,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    },
    {
      "lineItemId": "LI-001",
      "productId": "PROD-A1",
      "productName": "Premium Widget",
      "quantity": 2,
      "pricePerItem": 19.99,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    },
    {
      "lineItemId": "LI-003",
      "productId": "PROD-A2",
      "productName": "Deluxe Widget",
      "quantity": 1,
      "pricePerItem": 45,
      "distributor": {
        "id": "DIST-01",
        "name": "Widgets Inc.",
        "email": "orders@widgetsinc.com"
      }
    }
  ],
  "DIST-02": [
    {
      "lineItemId": "LI-002",
      "productId": "PROD-B1",
      "productName": "Standard Gadget",
      "quantity": 5,
      "pricePerItem": 5.5,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    },
    {
      "lineItemId": "LI-005",
      "productId": "PROD-B2",
      "productName": "Advanced Gadget",
      "quantity": 3,
      "pricePerItem": 15,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    },
    {
      "lineItemId": "LI-002",
      "productId": "PROD-B1",
      "productName": "Standard Gadget",
      "quantity": 5,
      "pricePerItem": 5.5,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    },
    {
      "lineItemId": "LI-005",
      "productId": "PROD-B2",
      "productName": "Advanced Gadget",
      "quantity": 3,
      "pricePerItem": 15,
      "distributor": {
        "id": "DIST-02",
        "name": "Gadget Supply Co.",
        "email": "sales@gadgetsupply.co"
      }
    }
  ],
  "DIST-03": [
    {
      "lineItemId": "LI-004",
      "productId": "PROD-C1",
      "productName": "Basic Thingamajig",
      "quantity": 10,
      "pricePerItem": 1.25,
      "distributor": {
        "id": "DIST-03",
        "name": "Things R Us",
        "email": "orders@thingsrus.net"
      }
    },
    {
      "lineItemId": "LI-004",
      "productId": "PROD-C1",
      "productName": "Basic Thingamajig",
      "quantity": 10,
      "pricePerItem": 1.25,
      "distributor": {
        "id": "DIST-03",
        "name": "Things R Us",
        "email": "orders@thingsrus.net"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![Mike\_Met](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mike_met/32/62227_2.png) [@Mike\_Met](https://community.glideapps.com/u/Mike_Met)\
**Post date:** [May 7, 2025, 6:38am UTC](https://community.glideapps.com/t/json-trebuchet/72744/22 "2025-05-07T06:38:59Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Mike\_Met](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mike_met/32/62227_2.png) [@Mike\_Met](https://community.glideapps.com/u/Mike_Met)\
**Post date:** [July 8, 2025, 10:18am UTC](https://community.glideapps.com/t/json-trebuchet/72744/23 "2025-07-08T10:18:18Z")

</div>

[  
{  
“distributorID”: “d00003”,  
“distributorEmail”: “[d3@mail.com](mailto:d3@mail.com)”,  
“items”: [  
{  
“name”: “Product 1”,  
“distributor”: “d3”,  
“qty”: 4,  
“price”: 899.94  
},  
{  
“name”: “Product 2”,  
“distributor”: “d3”,  
“qty”: 2,  
“price”: 94.99  
},  
{  
“name”: “Product 3”,  
“distributor”: “d3”,  
“qty”: 1,  
“price”: 899.94  
}  
]  
},  
{  
“distributorID”: “d00005”,  
“distributorEmail”: “[d5@mail.com](mailto:d5@mail.com)”,  
“items”: [  
{  
“name”: “Product 4”,  
“distributor”: “d5”,  
“qty”: 3,  
“price”: 540  
}  
]  
}  
]

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [July 8, 2025, 10:26am UTC](https://community.glideapps.com/t/json-trebuchet/72744/24 "2025-07-08T10:26:01Z")

</div>

There is a structural issue with your JSON, which makes it invalid - the second distributor has a nested object of items as opposed to an array.

Here is a corrected version with standard quotes instead of smart quotes (which also break it):

```auto
[
  {
    "distributorID": "d00003",
    "distributorEmail": "d3@mail.com",
    "items": [
      {
        "name": "Product 1",
        "distributor": "d3",
        "qty": 4,
        "price": 899.94
      },
      {
        "name": "Product 2",
        "distributor": "d3",
        "qty": 2,
        "price": 94.99
      },
      {
        "name": "Product 3",
        "distributor": "d3",
        "qty": 1,
        "price": 899.94
      }
    ]
  },
  {
    "distributorID": "d00005",
    "distributorEmail": "d5@mail.com",
    "items": [
      {
        "name": "Product 4",
        "distributor": "d5",
        "qty": 3,
        "price": 540
      }
    ]
  }
]

```

---

<div class="post-metadata">

**Author:** ![Mike\_Met](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mike_met/32/62227_2.png) [@Mike\_Met](https://community.glideapps.com/u/Mike_Met)\
**Post date:** [July 8, 2025, 10:43am UTC](https://community.glideapps.com/t/json-trebuchet/72744/25 "2025-07-08T10:43:23Z")

</div>

Thanks, I trimmed a longer version for the example and likely broke it.

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [July 8, 2025, 11:47pm UTC](https://community.glideapps.com/t/json-trebuchet/72744/26 "2025-07-08T23:47:14Z")

</div>

Do you need any further help here to query the JSON?

---

<div class="post-metadata">

**Author:** ![Mike\_Met](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/mike_met/32/62227_2.png) [@Mike\_Met](https://community.glideapps.com/u/Mike_Met)\
**Post date:** [July 14, 2025, 6:33am UTC](https://community.glideapps.com/t/json-trebuchet/72744/27 "2025-07-14T06:33:37Z")

</div>

I managed to figure it out using a sub-loop, thanks for the help.

[Previous page](https://community.glideapps.com/t/json-trebuchet/72744.md?page=1)
