how to parse JSON with single and double quotes in ruby on rails -


i having issue if name comes in ' or " json parsing fails. want know how can avoid this.

this current code:

def create  puts params  #@contact = contact.new(contact_params) #@contact.user = current_user  contactsparam = params[:contacts]  if contactsparam    # contactsparam.each |contact|   # end    contactvalues = contactsparam.map { |c|       puts "dulce!!! : " + c.to_s      json = json.parse(c)     result = "('#{json['name']}', '#{json['phone_number']}', '#{json['detail']}', '#{json['image_url']}', '#{json['email']}', '#{json['address']}', '#{json['city']}', '#{json['state']}', '#{json['zipcode']}', '#{json['country']}', #{current_user.id}, now(), now())"      result   }.join(",")    if contactvalues.length > 0      activerecord::base.connection.execute("insert contacts (name, phone_number, detail, image_url, email, address, city, state, zipcode, country, user_id, created_at, updated_at) values #{contactvalues}")   end end 

here example of json string fails (i added bunch of random characters test).

   {"name":"aaacontacttest'/'@"-jgg&$;$/&/@.'sheh","phone_number":",7*#;878545848487849648","detail":"","image_url":"","email":"test@test.com","address":"-/:;()$@""":$;$:$$/!:!/!,!6677bhgv 2017-07-25t20:08:54.614283+00:00 app[web.1]: hsbsbsbb7788$!","city":"hehshdbdb","state":"hsshhshs$&:$:$","zipcode":"3319)","country":"united states"} 

1.

let's first correct json object gave:

  {     "name":"aaacontacttest'/'@"-jgg&$;$/&/@.'sheh",     "phone_number":",7*#;878545848487849648",     "detail":"",     "image_url":"",     "email":"test@test.com",     "address":"-/:;()$@""":$;$:$$/!:!/!,!6677bhgv2017-07-25t20:08:54.614283+00:00 app[web.1]: hsbsbsbb7788$!",     "city":"hehshdbdb",     "state":"hsshhshs$&:$:$",     "zipcode":"3319)",     "country":"united states" } 

the problem json object is, not closed properly. if wanted use quotes in object please use backslash omit close value of object.

let's convert above invalid json object valid json object first, such ruby avoid syntax error:

 {     "name":"aaacontacttest'/'@\"-jgg&$;$/&/@.'sheh",     "phone_number":",7*#;878545848487849648",     "detail":"",     "image_url":"",     "email":"test@test.com",     "address":"-/:;()$@\"\"\":$;$:$$/!:!/!,!6677bhgv2017-07-25t20:08:54.614283+00:00 app[web.1]: hsbsbsbb7788$!",     "city":"hehshdbdb",     "state":"hsshhshs$&:$:$",     "zipcode":"3319)",     "country":"united states" } 

what did on here: initial hash object "name" key has value:

"name":"aaacontacttest'/'@"-jgg&$;$/&/@.'sheh"

area bold depicting have closed value quotes rest of italic character marked invalid.

types of errors ruby raised either "syntax error" or unexpected 'any char', expecting end-of-input

we have introduced backslash in value ensure proper closing of value.

"name":"aaacontacttest'/'@\"-jgg&$;$/&/@.'sheh"

so ever have used quotes in value please omit them using backslash.

  1. the object gave normal hash object not json object in case of ruby, ruby json parser expects argument string , can’t convert objects hash or array.

if json.generate(hash) json object:

note: hash if corrected hashobject.

adding json object on here.

"{\"name\":\"aaacontacttest'/'@\\"-jgg&$;$/&/@.'sheh\",\"phone_number\":\",7*#;878545848487849648\",\"detail\":\"\",\"image_url\":\"\",\"email\":\"test@test.com\",\"address\":\"-/:;()$@\\"\\"\\":$;$:$$/!:!/!,!6677bhgv2017-07-25t20:08:54.614283+00:00 app[web.1]: hsbsbsbb7788$!\",\"city\":\"hehshdbdb\",\"state\":\"hsshhshs$&:$:$\",\"zipcode\":\"3319)\",\"country\":\"united states\"}"

if json.parse(hashobject not jsonobject) ruby throw error:

typeerror: no implicit conversion of hash string

summary:

  1. correct hashobject.

  2. convert hashobject valid json object ruby. if wanted generate json use json.generate(hash) method.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -