Create posts from json source

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.

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

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.

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" 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.

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

@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

@rdyar Please explain how to create posts using javascript

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:

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

bundle exec jekyll build --drafts

# (or)

bundle exec jekyll serve --drafts

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

@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)

fixed by using entry['name']