# Create posts from json source

**URL:** <https://talk.jekyllrb.com/t/create-posts-from-json-source/2454>\
**Category:** Help\
**Created:** [December 28, 2018, 8:37am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454 "2018-12-28T08:37:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpmottathai](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bpmottathai/32/1434_2.png) [@bpmottathai](https://talk.jekyllrb.com/u/bpmottathai)\
**Post date:** [December 28, 2018, 8:37am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/1 "2018-12-28T08:37:27Z")

</div>

Hi,

I have a json source like,  
{  
“values”: [  
[“content”, “title link”, “image link”, “title name”],  
[“content”, “title link”, “image link”, “title name”],  
[“content”, “title link”, “image link”, “title name”],  
[“content”, “title link”, “image link”, “title name”],

```
]

```

}

Can I make normal Jekyll posts from this, if yes please explain. Thanks.

---

<div class="post-metadata">

**Author:** ![Frank](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/frank/32/26_2.png) [@Frank](https://talk.jekyllrb.com/u/Frank)\
**Post date:** [December 28, 2018, 7:53pm UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/2 "2018-12-28T19:53:56Z")

</div>

What do you mean by normal Jekyll posts?

If the question is can I access data stored in JSON (or YAML, or CSV) files, the answer is yes.

See [https://jekyllrb.com/docs/datafiles/#the-data-folder](https://jekyllrb.com/docs/datafiles/#the-data-folder)

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [December 29, 2018, 3:13am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/3 "2018-12-29T03:13:10Z")

</div>

you could access it from the data file in an existing page, but just putting that in the data folder would not create a new page/post.

You could use JS for that though. Not uncommon to use jekyll to create a json file and then have JS handle the display of it. If you already have the json file you are already half way there.

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [December 29, 2018, 11:25am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/4 "2018-12-29T11:25:29Z")

</div>

> [@bpmottathai](#):
>
> Can I make normal Jekyll posts from this

If the JSON source is data exported from a widely used blogging platform (e.g. tumblr), a plugin called `jekyll-import` might help you out. Take a look at [available \*importers"](https://import.jekyllrb.com/) and how to use them.

Otherwise, you’d have to write a _custom Ruby script_ that would create the files for you.  
For example, the following script will create “drafts” within a `_drafts` directory.

```ruby
require 'fileutils'
require 'json'

# Load JSON data from source
# (Assuming the data source is a json file on your file system)
data = JSON.parse(File.read(your_json_source.json))

# Proceed to create post files if the value array is not empty
array = data["values"]
if array && !array.empty?
  # create the `_drafts` directory if it doesn't exist already
  drafts_dir = File.expand_path('./_drafts', __dir__ )
  FileUtils.mkdir_p(posts_dir) unless Dir.exist?(drafts_dir)

  # iterate through the array and generate draft-files for each entry
  # where entry.first will be the "content" and entry.last the "title"
  array.each do |entry|
    File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
      draft.puts("---\n---\n\n#{entry.first}"
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![justinechacko](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/justinechacko/32/675_2.png) [@justinechacko](https://talk.jekyllrb.com/u/justinechacko)\
**Post date:** [December 31, 2018, 11:50am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/5 "2018-12-31T11:50:18Z")

</div>

@ashmaroli Can you please explain a littile bit more. In which filename and where should I save your given code and how to run it. My source is not from available importers. My source is [https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF\_lzG2WM-sjrpZIr3s02HrICBQ/exec](https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec)

@rdyar Please explain how to create posts using javascript

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [December 31, 2018, 1:44pm UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/6 "2018-12-31T13:44:59Z")

</div>

Simply copy that code into a new file with `.rb` extension somwhere in your source directory e.g., `_scripts/import.rb`, and then run it with Ruby:

```auto
ruby _scripts/import.rb

```

This will create a `_drafts` folder with draft files from the `values` array in the JSON data source from OP. Simply build your Jekyll site with the `--drafts` switch to render those drafts

```auto
bundle exec jekyll build --drafts

# (or)

bundle exec jekyll serve --drafts

```

---

<div class="post-metadata">

**Author:** ![justinechacko](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/justinechacko/32/675_2.png) [@justinechacko](https://talk.jekyllrb.com/u/justinechacko)\
**Post date:** [December 31, 2018, 1:57pm UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/7 "2018-12-31T13:57:27Z")

</div>

Thanks, can you tell me how to load json from the link to file?

---

<div class="post-metadata">

**Author:** ![bpmottathai](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bpmottathai/32/1434_2.png) [@bpmottathai](https://talk.jekyllrb.com/u/bpmottathai)\
**Post date:** [December 31, 2018, 5:24pm UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/8 "2018-12-31T17:24:26Z")

</div>

@ashmaroli, At present my code is  
require ‘fileutils’  
require ‘json’  
require ‘open-uri’

```
# Load JSON data from source
# (Assuming the data source is a json file on your file system)
data = JSON.load(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))

# Proceed to create post files if the value array is not empty
array = data["user"]
if array && !array.empty?
  # create the `_drafts` directory if it doesn't exist already
  drafts_dir = File.expand_path('./_drafts', __dir__ )
  FileUtils.mkdir_p(drafts_dir) unless Dir.exist?(drafts_dir)

  # iterate through the array and generate draft-files for each entry
  # where entry.first will be the "content" and entry.last the "title"
  array.each do |entry|
    File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
      draft.puts("---\n---\n\n#{entry.first}")
    end
  end
end

```

I have an error

```
2: from _scripts/import.rb:18:in `<main>'
	1: from _scripts/import.rb:18:in `each'
_scripts/import.rb:19:in `block in <main>': undefined method `last' for #<Hash:0x00005601afba0598> (NoMethodError)

```

Edit: I changed `JSON.load` to `JSON.parse`. Now I have error like

```
3: from _scripts/import.rb:7:in `<main>'
    2: from /usr/lib/ruby/2.5.0/json/common.rb:156:in `parse'
    1: from /usr/lib/ruby/2.5.0/json/common.rb:156:in `new'
    /usr/lib/ruby/2.5.0/json/common.rb:156:in `initialize': no implicit conversion of StringIO into String (TypeError)
```

---

<div class="post-metadata">

**Author:** ![justinechacko](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/justinechacko/32/675_2.png) [@justinechacko](https://talk.jekyllrb.com/u/justinechacko)\
**Post date:** [January 1, 2019, 10:21am UTC](https://talk.jekyllrb.com/t/create-posts-from-json-source/2454/9 "2019-01-01T10:21:32Z")

</div>

fixed by using `entry['name']`
