Skip to content

Instantly share code, notes, and snippets.

@mashaal
Created September 13, 2018 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mashaal/63a4ae09350d0a575ae85e6d3d112b9a to your computer and use it in GitHub Desktop.
Save mashaal/63a4ae09350d0a575ae85e6d3d112b9a to your computer and use it in GitHub Desktop.
Contentful Cleaner
import traverse from 'traverse'
const cleaner = obj =>
traverse(obj).map(function() {
if (this.node.fields && this.node.sys) this.update(this.node.fields)
})
export default cleaner
@mashaal
Copy link
Author

mashaal commented Sep 13, 2018

This will recursively strip out sys objects, and collapse the field objects. It's a bit easier to work with, and great for nested references.

Original output (with nested references)

{
	"sys": {
		"space": {
			"sys": {
				"type": "Link",
				"linkType": "Space",
				"id": "vla9qt2l9qkq"
			}
		},
		"id": "6ADKqqJu3mO8GSAyYgkgc0",
		"type": "Entry",
		"createdAt": "2018-09-13T01:43:59.892Z",
		"updatedAt": "2018-09-13T23:34:45.668Z",
		"environment": {
			"sys": {
				"id": "master",
				"type": "Link",
				"linkType": "Environment"
			}
		},
		"revision": 4,
		"contentType": {
			"sys": {
				"type": "Link",
				"linkType": "ContentType",
				"id": "programPage"
			}
		},
		"locale": "en-US"
	},
	"fields": {
		"title": "Page",
		"nestedReferences": [
			{
				"sys": {
					"space": {
						"sys": {
							"type": "Link",
							"linkType": "Space",
							"id": "vla9qt2l9qkq"
						}
					},
					"id": "6OHRx4MzW80CSK20io266c",
					"type": "Entry",
					"createdAt": "2018-09-13T23:34:19.162Z",
					"updatedAt": "2018-09-13T23:34:19.162Z",
					"environment": {
						"sys": {
							"id": "master",
							"type": "Link",
							"linkType": "Environment"
						}
					},
					"revision": 1,
					"contentType": {
						"sys": {
							"type": "Link",
							"linkType": "ContentType",
							"id": "programListing"
						}
					},
					"locale": "en-US"
				},
				"fields": {
					"title": "Hello World",
					"slug": "hello-world",
					"content": ":)"
				}
			},
			{
				"sys": {
					"space": {
						"sys": {
							"type": "Link",
							"linkType": "Space",
							"id": "vla9qt2l9qkq"
						}
					},
					"id": "1gy9crcgCAe8MeuMS0KqYi",
					"type": "Entry",
					"createdAt": "2018-09-13T23:34:41.540Z",
					"updatedAt": "2018-09-13T23:34:41.540Z",
					"environment": {
						"sys": {
							"id": "master",
							"type": "Link",
							"linkType": "Environment"
						}
					},
					"revision": 1,
					"contentType": {
						"sys": {
							"type": "Link",
							"linkType": "ContentType",
							"id": "programListing"
						}
					},
					"locale": "en-US"
				},
				"fields": {
					"title": "Foo",
					"slug": "foo",
					"content": "Bar"
				}
			}
		]
	}
}

Cleaned output

{
	"title": "Page",
	"nestedReferences": [
		{
			"title": "Hello World",
			"slug": "hello-world",
			"content": ":)"
		},
		{
			"title": "Foo",
			"slug": "foo",
			"content": "Bar"
		}
	]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment