error parsing document from spark to MongoDB, JSON reader was expecting a value ..

1,474 views
Skip to first unread message

Mich Talebzadeh

unread,
Sep 3, 2018, 4:00:28 AM9/3/18
to mongod...@googlegroups.com
Hi,

I am trying to parse a string using the following:

scala> var sparkDocuments : org.bson.Document = null
sparkDocuments: org.bson.Document = null
scala> for(line <- text)
     |  {
     | random_string = line._5
     | println(random_string)
     | sparkDocuments = Document.parse(s"{random_string: $random_string}")
     | println (sparkDocuments)
     | }
NradByNwlTGowxCyzCjHNZZtA
org.bson.json.JsonParseException: JSON reader was expecting a value but found 'NradByNwlTGowxCyzCjHNZZtA'.
  at org.bson.json.JsonReader.readBsonType(JsonReader.java:251)
  at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149)
  at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
  at org.bson.Document.parse(Document.java:105)
  at org.bson.Document.parse(Document.java:90)
  at $anonfun$1.apply(<console>:48)
  at $anonfun$1.apply(<console>:44)
  at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
  at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
  ... 63 elided

This works for numeric fields but not for strings!.

Thanks



Dr Mich Talebzadeh

 

LinkedIn  https://www.linkedin.com/profile/view?id=AAEAAAAWh2gBxianrbJd6zP6AcPCCdOABUrV8Pw

 

http://talebzadehmich.wordpress.com


Disclaimer: Use it at your own risk. Any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on this email's technical content is explicitly disclaimed. The author will in no case be liable for any monetary damages arising from such loss, damage or destruction.

 

Wan Bachtiar

unread,
Sep 3, 2018, 10:17:06 PM9/3/18
to mongodb-user

This works for numeric fields but not for strings

Hi Mich,

You need quotes to wrap the Scala string variable. For example:

sparkDocuments = org.bson.Document.parse(s"{a: '$randomString'}")
sparkDocuments: org.bson.Document = Document{{a=NradByNwlTGowxCyzCjHNZZtA}}

Regards,
Wan.

Mich Talebzadeh

unread,
Sep 4, 2018, 3:36:01 AM9/4/18
to mongod...@googlegroups.com
Many thanks Wan. very helpful.

Regards,

Dr Mich Talebzadeh

 

LinkedIn  https://www.linkedin.com/profile/view?id=AAEAAAAWh2gBxianrbJd6zP6AcPCCdOABUrV8Pw

 

http://talebzadehmich.wordpress.com


Disclaimer: Use it at your own risk. Any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on this email's technical content is explicitly disclaimed. The author will in no case be liable for any monetary damages arising from such loss, damage or destruction.

 


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/950f77a9-9ee0-4278-a985-65ae6a0c97fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages