no implicit conversion of StringIO into String (TypeError) - ruby












1















I have a script called import.rb which will import json content from url to drafts directory in jekyll. Below is my code.



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.parse(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---nn#{entry.first}")
end
end
end


When I run ruby _scripts/import.rb I get 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)


Please suggest the corrections.










share|improve this question





























    1















    I have a script called import.rb which will import json content from url to drafts directory in jekyll. Below is my code.



    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.parse(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---nn#{entry.first}")
    end
    end
    end


    When I run ruby _scripts/import.rb I get 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)


    Please suggest the corrections.










    share|improve this question



























      1












      1








      1








      I have a script called import.rb which will import json content from url to drafts directory in jekyll. Below is my code.



      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.parse(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---nn#{entry.first}")
      end
      end
      end


      When I run ruby _scripts/import.rb I get 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)


      Please suggest the corrections.










      share|improve this question
















      I have a script called import.rb which will import json content from url to drafts directory in jekyll. Below is my code.



      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.parse(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---nn#{entry.first}")
      end
      end
      end


      When I run ruby _scripts/import.rb I get 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)


      Please suggest the corrections.







      json ruby jekyll






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 8:28







      Fun Media

















      asked Jan 1 at 8:08









      Fun MediaFun Media

      226




      226
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Change this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))


          To this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec').string)


          The .string method Returns underlying String object.



          And while you're at it, change this:



          array && !array.empty?


          To this:



          array&.any?


          &. is the safe navigation operator and it simplifies the process of checking for nil and calling a method on the object. And from a style perspective, it's preferred to call array.any? over !array.empty?.



          Finally, when using FileUtils.mkdir_p you do not have to include the guard condition unless Dir.exist?(drafts_dir). It can be safely called without concern that it will remove or overwrite the existing directory.






          share|improve this answer


























          • Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

            – Fun Media
            Jan 1 at 8:47











          • I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

            – anothermh
            Jan 1 at 9:23











          • Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

            – Fun Media
            Jan 1 at 9:48











          • fixed it by removing link in the entry['name']

            – Fun Media
            Jan 1 at 10:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993965%2fno-implicit-conversion-of-stringio-into-string-typeerror-ruby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Change this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))


          To this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec').string)


          The .string method Returns underlying String object.



          And while you're at it, change this:



          array && !array.empty?


          To this:



          array&.any?


          &. is the safe navigation operator and it simplifies the process of checking for nil and calling a method on the object. And from a style perspective, it's preferred to call array.any? over !array.empty?.



          Finally, when using FileUtils.mkdir_p you do not have to include the guard condition unless Dir.exist?(drafts_dir). It can be safely called without concern that it will remove or overwrite the existing directory.






          share|improve this answer


























          • Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

            – Fun Media
            Jan 1 at 8:47











          • I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

            – anothermh
            Jan 1 at 9:23











          • Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

            – Fun Media
            Jan 1 at 9:48











          • fixed it by removing link in the entry['name']

            – Fun Media
            Jan 1 at 10:18
















          0














          Change this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))


          To this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec').string)


          The .string method Returns underlying String object.



          And while you're at it, change this:



          array && !array.empty?


          To this:



          array&.any?


          &. is the safe navigation operator and it simplifies the process of checking for nil and calling a method on the object. And from a style perspective, it's preferred to call array.any? over !array.empty?.



          Finally, when using FileUtils.mkdir_p you do not have to include the guard condition unless Dir.exist?(drafts_dir). It can be safely called without concern that it will remove or overwrite the existing directory.






          share|improve this answer


























          • Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

            – Fun Media
            Jan 1 at 8:47











          • I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

            – anothermh
            Jan 1 at 9:23











          • Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

            – Fun Media
            Jan 1 at 9:48











          • fixed it by removing link in the entry['name']

            – Fun Media
            Jan 1 at 10:18














          0












          0








          0







          Change this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))


          To this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec').string)


          The .string method Returns underlying String object.



          And while you're at it, change this:



          array && !array.empty?


          To this:



          array&.any?


          &. is the safe navigation operator and it simplifies the process of checking for nil and calling a method on the object. And from a style perspective, it's preferred to call array.any? over !array.empty?.



          Finally, when using FileUtils.mkdir_p you do not have to include the guard condition unless Dir.exist?(drafts_dir). It can be safely called without concern that it will remove or overwrite the existing directory.






          share|improve this answer















          Change this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'))


          To this:



          data = JSON.parse(open('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec').string)


          The .string method Returns underlying String object.



          And while you're at it, change this:



          array && !array.empty?


          To this:



          array&.any?


          &. is the safe navigation operator and it simplifies the process of checking for nil and calling a method on the object. And from a style perspective, it's preferred to call array.any? over !array.empty?.



          Finally, when using FileUtils.mkdir_p you do not have to include the guard condition unless Dir.exist?(drafts_dir). It can be safely called without concern that it will remove or overwrite the existing directory.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 8:39

























          answered Jan 1 at 8:33









          anothermhanothermh

          3,30931633




          3,30931633













          • Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

            – Fun Media
            Jan 1 at 8:47











          • I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

            – anothermh
            Jan 1 at 9:23











          • Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

            – Fun Media
            Jan 1 at 9:48











          • fixed it by removing link in the entry['name']

            – Fun Media
            Jan 1 at 10:18



















          • Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

            – Fun Media
            Jan 1 at 8:47











          • I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

            – anothermh
            Jan 1 at 9:23











          • Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

            – Fun Media
            Jan 1 at 9:48











          • fixed it by removing link in the entry['name']

            – Fun Media
            Jan 1 at 10:18

















          Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

          – Fun Media
          Jan 1 at 8:47





          Thanks, Now I get error like 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:0x00005600277297a0> (NoMethodError)

          – Fun Media
          Jan 1 at 8:47













          I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

          – anothermh
          Jan 1 at 9:23





          I assume that you're trying to read the value of the name key in the hash (the URL like https://www.flipkart.com/...). In that case, change entry.last to entry['name'], or entry.fetch('name').

          – anothermh
          Jan 1 at 9:23













          Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

          – Fun Media
          Jan 1 at 9:48





          Thanks, now error comes like No such file or directory @ rb_sysopen - /home/_scripts/_drafts/https://www.flipkart.com/redmi-note-6-pro-black-64-gb/p/itmfayzxqnzjhtbh?affid=xxx,[Flipkart] Redmi Note 6 Pro (Black, 64 GB)  (4 GB RAM) (Errno::ENOENT)

          – Fun Media
          Jan 1 at 9:48













          fixed it by removing link in the entry['name']

          – Fun Media
          Jan 1 at 10:18





          fixed it by removing link in the entry['name']

          – Fun Media
          Jan 1 at 10:18




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993965%2fno-implicit-conversion-of-stringio-into-string-typeerror-ruby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$