Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Data to Contentful programatically from a json file

I am trying to import some data programatically into contentful:

I am following the docs here

And running the command inside my integrated terminal

contentful space import --config config.json  

Where the config file is

{
  "spaceId": "abc123",
  "managementToken": "112323132321adfWWExample",
  "contentFile": "./dataToImport.json"
}

And the dataToImport.json file is

{
  "data": [
    {
      "address": "11234 New York City"
    },
    {
      "address": "1212 New York City"
    }
  ]
}

The thing is I don't understand what format my dataToImport.json should be and what is missing inside this file or in my config file so that the array of addresses from the .json file get added as new entries to an already created content model inside the Contentful UI show in the screenshot below

enter image description here

I am not specifying the content model for the data to go into so I believe that is one issue, and I don't know how I do that. An example or repo would help me out greatly

like image 573
Aquasar Avatar asked Dec 01 '25 14:12

Aquasar


1 Answers

The types of data you can import are listed : in their documentation

your json top level should say "entries" and not data, if new content of a content type is what you would like to import.

This is an example of a blog post as per content model of the tutorial they provide. The only thing i didn't work out yet is where the user id is :D so i substituted for one of the content type 'person' also provided in their tutorial (I think it's called Gatsby Starter)

{"entries": [
{
  "sys": {
    "space": {
      "sys": {
        "type": "Link",
        "linkType": "Space",
        "id": "theSpaceIdToReceiveYourImport"
      }
    },    
    "type": "Entry",
    "createdAt": "2019-04-17T00:56:24.722Z",
    "updatedAt": "2019-04-27T09:11:56.769Z",
    "environment": {
      "sys": {
        "id": "master",
        "type": "Link",
        "linkType": "Environment"
      }
    },
    "publishedVersion": 149, -- these are not compulsory, you can skip 
    "publishedAt": "2019-04-27T09:11:56.769Z", --  you can skip
    "firstPublishedAt": "2019-04-17T00:56:28.525Z", --  you can skip
    "publishedCounter": 3, --  you can skip
    "version": 150,
    "publishedBy": { -- this is an example of a linked content
      "sys": {
        "type": "Link",
        "linkType": "person",
        "id": "personId"
      }
    },
    "contentType": {
      "sys": {
        "type": "Link",
        "linkType": "ContentType",
        "id": "blogPost" -- here should be your content type 'RealtorProperties'
      }
    }
  },
  "fields": { -- here should go your content type fields, i can't see it in your post
            "title": {
                "en-US": "Test 1"
            },
            "slug": {
                "en-US": "Test-1"
            },
            
            "description": {
                "en-US": "some description"
            },
            "body": {
                "en-US": "some body..."
            },
           
            "publishDate": {
                "en-US": "2016-12-19"
            },  
            "heroImage": { -- another example of a linked content
              "en-US": {
                "sys": {
                  "type": "Link",
                  "linkType": "Asset",
                  "id": "idOfTHisImage"
                }
                }
            }
  }
},
--another entry, ...]}
like image 55
P_R Avatar answered Dec 03 '25 18:12

P_R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!