Create specific JSON object from collections?

I have a google maps data structure that my google map embedded in a web page relies on and I’d like to turn the json into collections where you can add the locations dynamically, but I dont see a way to do that. I know there’s a json object field in cockpit, but I’d like to make items within the json itself dynamic.

Does anyone know how I can use collections to have it create JSON in this format for example?

{
  "restaurants": [
      {
        "name" : "Restaurant 1",
        "lat" : XX.XXXXX,
        "lng" : -XX.XXXXX,
        "group" : "restaurants"
      },
      {
        "name" : "Restaurant 2",
        "lat" : XX.XXXXX,
        "lng" : XX.XXXXX,
        "group" : "restaurants"
      }
  ],

  "retail" : [
            {
              "name": "Retail 1",
              "lat": XX.XXXXX,
              "lng": XX.XXXXX,
              "group": "retail"
            },
            {
              "name": "Retail 2",
              "lat": XX.XXXXX,
              "lng" : XX.XXXXX,
              "group": "retail"
            }
  ]

So, I had to do something similar and used webhooks to do it.
For a specific collection, I added a webhook in Cockpit with the event: collections.save.before.my_collection
and the url pointing to a php file on my server that makes a call to the API, transforms the response and writes to a a new json file.

In your case, you can transform the api response into the JSON you need and then point the maps code to that created JSON file.

Hmm interesting. What is the purpose of the web hook. Based on your explanation I assume I can simply write my own php file that reads a collection of map points from the API and then I output the returned json to json again with a new structure?

The webhook triggers on an event, so I use it to rebuild the static json any time I save within the collection. So, if I update content, I know that the static json is in sync with my changes in cockpit.

And as far as your question, this is the structure of what I’m doing 9but in short answer, yes):

  • Webhook that triggers on save of content in a specific collection
  • The webhook points to a file on my server
  • The file on my server makes an API call to the collection, remaps the JSON response with a new structure, and spits out a new JSON file that I reference

I see what you mean I think because you’re actually creating a json file after through the web hook.

But I was thinking based off the concept of your post I can just create a php file that creates json based off of some collections through the cockpit collections api and then point to that php file to read the json structure I need. In that case I wouldn’t need to keep updating json through the web hook because everytime a request came to the php file it will create json based off of a collection.