Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:51:31+00:00 2026-06-12T13:51:31+00:00

I am trying to read in a JSON file with Ruby and the output

  • 0

I am trying to read in a JSON file with Ruby and the output is extremely strange. Here is the code that I am using:

require 'rubygems'

class ServiceCalls 

    def initialize ()

    end

    def getFile()

        Dir.entries('./json').each do |mFile|

            if mFile[0,1] != "."
                self.sendServiceRequest(mFile)
            end

        end
    end

    def sendServiceRequest(mFile)
        currentFile = File.new("./json/" + mFile, "r") 
        puts currentFile.read
        currentFile.close
    end



end

mServiceCalls = ServiceCalls.new
mServiceCalls.getFile

And here is the output:

Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094AA9DAB6D??/??is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG
bookmark88?A[DT>??A?@
                     ApplicationsMAMPhtdocsServiceTestAutomationMDXservicecatalog-verizon.json$4T??
                  `?
                   U?????l??????
                                Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094?is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG
UIEvolutions-MacBook-Pro-109:MDXServiceTesting Banderson$ ruby testmdxservices.rb
bookmark88?A?,P>??A?@
                     ApplicationsMAMPhtdocsServiceTestAutomationMDXservicecatalog-adaptation.json$4T??
                     `?
                      U?????l??????
                                   Macintosh H??=A?v?P$66267945-2481-3907-B88A-1094AA9DAB6D??/?<icns<?TOC his32?s8mic118il32?l8mic1?ic07ic13#ic08#ic14^?ic09_ic1?is32???????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz?????????????????????????????????????????????????????????????????????????????????????????????vvz???????????????????????????????????????????????????????????s8m+88888888???????89????????99?????????9:??????????:;??????????;=??????????=>??????????>????????????@??????????@A??????????AC??????????CD??????????DE??????????EE??????????E6OXdknnkdXO6ic118?PNG

IHDR szz?iCCPICC Profile(?T?k?P??e???:g >h?ndStC??kW??Z?6?!H??m\??$?~?ًo:?w?>?
                                                                             كo{?a???"L?"???4M'S??????9'??^??qZ?/USO???????^C+?hM??J&G@Ӳy???lt?o߫?c՚?
                                                                    ? ??5?"?Y?i\?΁?'&??.?<?ER/?dE?oc?ግ#?f45@?   ??B:K?@8?i??
                  ??s??_???雭??m?N?|??9}p?????_?A??pX6?5~B?$?&???ti??e??Y)%$?bT?3li?
    ??????P???4?43Y???P??1???KF????ۑ??5>?)?@????r??y??????[?:V???ͦ#??wQ?HB??d(??B
                                                                               a?cĪ?L"J??itTy?8?;(???Gx?_?^?[???????%׎??ŷ??Q???麲?ua??n?7???
                                                           Q???H^e?O?Q?u6?S??u
                                                                              ?2??%vX
     ???^?*l
O?????ޭˀq,>??S???%?L??d????B???1CZ??$M??9??P
                                            'w????\/????]???.r#???E|!?3?>_?o?a?۾?d?1Z?ӑ???z???'?=??????~+??cjJ?tO%mN?????
                                         |??-???bW?O+
o?
  ^?
    I?H?.?;???S?]?i_s9?*p???.7U^??s.?3u?

Can someone please tell me what I am doing wrong? Do I need to specify what type of encoding I’m using? I have tried to read the file with gets, sysread, and another I can’t remember.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:51:32+00:00Added an answer on June 12, 2026 at 1:51 pm

    I am not completely sure why but I believe it is the ‘./json’ path that is causing the issue. I tried the script on my Windows XP machine and got similar results.

    However, when I rewrote the script to include File.dirname(__FILE__) instead of './' it worked. I also cleaned up some of the code.

    class ServiceCalls 
    
        def get_file
            dirname = File.join(File.dirname(__FILE__), 'json')
            Dir.entries(dirname).each do |file|
                unless file.start_with? '.'
                    File.open(File.join(dirname, file), 'r') {|f| puts f.read}
                end
            end
        end
    end
    
    sc = ServiceCalls.new
    sc.get_file
    

    __FILE__ is the path of the current script. File.join uses system independent path separators. File.open, if you pass it a block, will actually close the file for you when it completes the block. String#start_with? is a cleaner way than using [0,1] to get the first element of a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to edit a JSON file using javaScript? I'm trying to read
I'm trying to read a legacy JSON code using Jackson 2.0-RC3, however I'm stuck
I have a JSON object that I am trying to read using Python but
I have a JSON file here: http://dalexl.webs.com/products.json I'm trying to read it on my
I'm trying to read a JSON file into a data structure so that I
Hi I am trying to read in a .json file from my local machine.
I'm trying to read data into my calendar visualisation using JSON. At the moment
I'm trying to parse a local json file and the output is not what
I'm trying to get Jquery to read a json file, But unfortunately I can
I am trying to read a JSON file and parse it. I have this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.