How to import nested JSON

I have this JSON:
{
“id”: “12345”,
“name”: “Event title”,
“start_time”: “2020-03-12T23:00:00+0100”,
“end_time”: “2020-03-13T04:00:00+0100”,
“description”: “A general description of the event”,
“cover”: {
“offset_x”: 50,
“offset_y”: 50,
“source”: “https://URL_PATH/image.jpg”,
“id”: “6789”
}
}

I’ve set up a collection like this:

id Text,
name Text,
start_time Text,
end_time Text,
description Wysiwyg,
cover Set, with the following options:

    {
        "fields": [
          {
            "name": "offset_x",
            "type": "text"
          },
          {
            "name": "offset_y",
            "type": "text"
          },
          {
            "name": "source",
            "type": "text"
          },
          {
            "name": "id",
            "type": "text"
          }
    ]
  }

I imported the JSON but the cover options do not import.

Any help would be greatly appreciated :slight_smile:

Your set fields expects an array, but you saved an object.

If you transform your json to import into

{
"...": "...",
"cover":[{
  "...": "..."
}],
}

it should work (not tested).

Excellent. I will give that a shot.
I’ll report back later.

Thanks :smiley:

Well it does work :smiley:
Partially though, the data does get imported but the “cover” data is shown partially in the CMS.

It shows on the /collections/entries/events/ page (all entries):

import-set-fieldtype

But it doesn’t show on the /collections/entry/events/ page (single entry):

I also have a repeater field with a set field nested that has the same issue. The data is indeed imported but shows up partially in the CMS.

I have this event that occurs 12 times a year (every first sunday of the month).

import-set-fieldtype-3

Repeater field:

{
  "fields": [
    {
      "type": "set",
      "label": "Multiple dates",
      "options": {
        "fields": [
          {
            "name": "id",
            "type": "text",
            "display": "$value",
            "label": "$value"
          },
          {
            "name": "start_time",
            "type": "text",
            "display": "$value",
            "label": "$value"
          },
          {
            "name": "end_time",
            "type": "text",
            "display": "$value",
            "label": "$value"
          }
        ]
      }
    }
  ]
}

I tried both the display and label properties to no avail… :anguished:

I tried editing one of the properties (offset_x) of the cover to see whether it would reflect on the /collections/entries/events/ page.

Well it did reflect a change but not the change I hoped for. It changed the value for offset_x but erased the values of the other three properties:

import-set-fieldtype-5

So I added values for the other properties too and I noticed the “cover” array is converted to a “cover” object :confused:
from:
"cover": [{ "offset_x": "50", "offset_y": "50", "source": "https://bla.jpg?bla", "id": "12345" }]
to:
"cover": { "offset_x": "50", "offset_y": "50", "source": "https://bla.jpg?bla", "id": "12345" }

This would ofcourse mess up the frontend logic…

This is very confusing and I’d like to be able to check data in the cms dashboard, which isn’t possible for Sets and Repeaters for me right now.

This seems to be a problem of the data format of a repeater with multiple fields. If the repeater has multiple different fields - or one, but the possibility to have multiple - it has a weird data format that always adds the field definitions.

Create a test collection with two repeater fields. One with "fields": [{"...":"..."}] and one with "field":{"...":"..."}, add some dummy data and look at the data format (via api or if you are logged in as admin with the “Show JSON” button. So it makes a huge difference, if your repeater repeats a single field type (produces a nice and clean array) or multiple field types (produces something like [{"field":{...},"value":{...}}]).

If your imported data doesn’t match the expected data structure, it won’t display correctly.