Forum: Ruby JSON Parser

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b80a21309877bb28bd84d8afb65b04c?d=identicon&s=25 Rama Singh (rama)
on 2007-04-02 07:01
I am getting my data in JSON format, how can i validate or Parse JSON
data in rails.

Please help me,

Thanks & Regards,

Rama
F0afd024e17c0c4753aa8d618ba9bb0f?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2007-04-02 10:05
(Received via mailing list)
On Monday 02 April 2007 05:01, Rama Singh wrote:
> I am getting my data in JSON format, how can i validate or Parse JSON
> data in rails.
>
> Please help me,
>
> Thanks & Regards,
>
> Rama

RTFM?

but seriously - gogle for ruby JSON - first result gives you ruby JSON
library:

if you have data in JSON do @j = JSON.parse(json_data)
if you want to generate JSON from hash/array - json_data =
JSON.unparse(@hash)
19605c7ede229e507e894865308a6d53?d=identicon&s=25 Gustav Paul (Guest)
on 2007-04-02 10:41
(Received via mailing list)
Rama Singh wrote:
> I am getting my data in JSON format, how can i validate or Parse JSON
> data in rails.
>
> Please help me,
>
> Thanks & Regards,
>
> Rama
>
>
you could use the json gem

http://json.rubyforge.org/

HTH

Gustav Paul
3b80a21309877bb28bd84d8afb65b04c?d=identicon&s=25 Rama Singh (rama)
on 2007-04-02 10:43
Marcin Raczkowski wrote:
> On Monday 02 April 2007 05:01, Rama Singh wrote:
>> I am getting my data in JSON format, how can i validate or Parse JSON
>> data in rails.
>>
>> Please help me,
>>
>> Thanks & Regards,
>>
>> Rama
>
> RTFM?
>
> but seriously - gogle for ruby JSON - first result gives you ruby JSON
> library:
>
> if you have data in JSON do @j = JSON.parse(json_data)
> if you want to generate JSON from hash/array - json_data =
> JSON.unparse(@hash)

my code :
=================================
require 'D3I.rb'
require 'json'
status = 0
ic = nil
begin
    ic = Ice::initialize(ARGV)
    base = ic.stringToProxy("SessionFactory:tcp -p 10000")
    twoway = D3View::ISessionFactoryPrx::checkedCast(base)
    if not twoway
        raise "Invalid proxy"
    end

    session = twoway.CreateSession()
    olv = session.LoadObjects("APAL3DB", "AP3Customer")
    olvID, iNumRecs = olv.AsJSON(0, 2)

    #puts olvID
    @j = JSON.parse(olvID)
    puts @j
    puts '------------------------------------------------------------'

    #puts olv.GetID()
  rescue Ice::Exception => ex
    puts ex
  end
exit
==================================
In the above code "olvID" variable get JSON data, i want to check iam
getting valid JSON or not, for that i used "@j = JSON.parse(olvID)", but
i get error :

c:/ruby/lib/ruby/site_ruby/1.8/json.rb:300:in `parse_object': expected
',' or '}' in object at '"Val
ue":"",}},"MetaC'! (JSON::ParserError)
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:259:in `parse_value'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:296:in
`parse_object'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:259:in `parse_value'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:296:in
`parse_object'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:259:in `parse_value'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:296:in
`parse_object'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:259:in `parse_value'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:296:in
`parse_object'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:202:in `parse'
        from c:/ruby/lib/ruby/site_ruby/1.8/json.rb:442:in `parse'
        from client.rb:22
Don't know is it error in my JSON data, or in gems
D84df7c68f790e492c4ad4ec5fe65547?d=identicon&s=25 Florian Frank (Guest)
on 2007-04-02 12:53
(Received via mailing list)
Rama Singh wrote:
> In the above code "olvID" variable get JSON data, i want to check iam
> getting valid JSON or not, for that i used "@j = JSON.parse(olvID)", but
> i get error :
>
> c:/ruby/lib/ruby/site_ruby/1.8/json.rb:300:in `parse_object': expected
> ',' or '}' in object at '"Val
> ue":"",}},"MetaC'! (JSON::ParserError)
         ^
This isn't allowed in a valid JSON text.
3b80a21309877bb28bd84d8afb65b04c?d=identicon&s=25 Rama Singh (rama)
on 2007-04-02 13:01
Florian Frank wrote:
> Rama Singh wrote:
>> In the above code "olvID" variable get JSON data, i want to check iam
>> getting valid JSON or not, for that i used "@j = JSON.parse(olvID)", but
>> i get error :
>>
>> c:/ruby/lib/ruby/site_ruby/1.8/json.rb:300:in `parse_object': expected
>> ',' or '}' in object at '"Val
>> ue":"",}},"MetaC'! (JSON::ParserError)
>          ^
> This isn't allowed in a valid JSON text.

I think this time you understood what actually i am looking for :

1. I get AsJSON data.
2. I want to parse JSON data in ruby.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-02 13:57
(Received via mailing list)
On Apr 2, 2007, at 6:01 AM, Rama Singh wrote:

>>> ',' or '}' in object at '"Val
>>> ue":"",}},"MetaC'! (JSON::ParserError)
>>          ^
>> This isn't allowed in a valid JSON text.
>
> I think this time you understood what actually i am looking for :
>
> 1. I get AsJSON data.
> 2. I want to parse JSON data in ruby.

But as Florian said, you JSON data is not valid JSON.  That's why the
Ruby parser won't parse it.

James Edward Gray II
3b80a21309877bb28bd84d8afb65b04c?d=identicon&s=25 Rama Singh (rama)
on 2007-04-02 14:09
James Gray wrote:
> On Apr 2, 2007, at 6:01 AM, Rama Singh wrote:
>
>>>> ',' or '}' in object at '"Val
>>>> ue":"",}},"MetaC'! (JSON::ParserError)
>>>          ^
>>> This isn't allowed in a valid JSON text.
>>
>> I think this time you understood what actually i am looking for :
>>
>> 1. I get AsJSON data.
>> 2. I want to parse JSON data in ruby.
>
> But as Florian said, you JSON data is not valid JSON.  That's why the
> Ruby parser won't parse it.
>
> James Edward Gray II

Thanks  Florian.
Thanks James, I found the error in JSON txt.

My last question is there any online website, where i can paste my JSON
data into textarea and i can check from there regarding JSON validation.

Thanks

Rama
A8486651a974d7d9ecc35d25de96c7ee?d=identicon&s=25 Chris Lowis (chrislo)
on 2007-04-02 15:12
> My last question is there any online website, where i can paste my JSON
> data into textarea and i can check from there regarding JSON validation.

It's not a webiste, but you might want to look at this :
http://www.kuwata-lab.com/kwalify/

Chris
3b80a21309877bb28bd84d8afb65b04c?d=identicon&s=25 Rama Singh (rama)
on 2007-04-02 15:31
Chris Lowis wrote:
>> My last question is there any online website, where i can paste my JSON
>> data into textarea and i can check from there regarding JSON validation.
>
> It's not a webiste, but you might want to look at this :
> http://www.kuwata-lab.com/kwalify/
>
> Chris

Chris :

Thanks for your TIP, i liked the link you send to me, but in kwalify, it
validates for YAML file, there it is mentioned that YAML and JSON are
some what similar, & moreover it validates from schema, in my
application i have not written any schema.

I just wanted to check my JSON data is valid or not, either from website
or from command prompt.

Regards,

Rama
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-04-02 16:56
(Received via mailing list)
On Apr 2, 7:31 am, Rama Singh <rama.sing...@gmail.com> wrote:
> I just wanted to check my JSON data is valid or not,
> either from website or from command prompt.

If you want to know if it is valid, and you have access to a JS
interpreter (e.g. a web browser) then you could simply eval() it and
see if it compiles or not.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-04-02 16:57
(Received via mailing list)
On Apr 2, 8:51 am, "Phrogz" <g...@refinery.com> wrote:
> On Apr 2, 7:31 am, Rama Singh <rama.sing...@gmail.com> wrote:
>
> > I just wanted to check my JSON data is valid or not,
> > either from website or from command prompt.
>
> If you want to know if it is valid, and you have access to a JS
> interpreter (e.g. a web browser) then you could simply eval() it and
> see if it compiles or not.

For example, go to:
http://phrogz.net/tmp/simplejs.html

and paste your JSON 'code' in, and then hit tab or otherwise defocus
the top pane. If you see valid data in the bottom, the input was
valid. If not, well, it's not a valid JS object.
F0afd024e17c0c4753aa8d618ba9bb0f?d=identicon&s=25 Marcin Raczkowski (Guest)
on 2007-04-03 00:27
(Received via mailing list)
On Monday 02 April 2007 12:09, Rama Singh wrote:
> >> 1. I get AsJSON data.
> My last question is there any online website, where i can paste my JSON
> data into textarea and i can check from there regarding JSON validation.
>
> Thanks
>
> Rama

~ irb
require 'json'

begin
  @l = File.open($*[0], "r") { |f| f.readlines }
rescue Exception
  puts "could not open file"
end

begin
  JSON.unparse(@l)
  puts "it's valid JSON"
rescue Exception
  puts "it's not valid JSON"
end
This topic is locked and can not be replied to.