Mongodb - invalid json output

I’m using MongoDBLibrary and I can query the database BUT I get the output as invalid json .
How can I retrieve / convert the output to valid json?

Will appreciate your assistance

Hi Moshe,

Can you show us what you are trying and how you are querying the result, e.g. which libraries are you using? RequestsLibrary?

Some applications need you to set Accept: application/json header to get a json response otherwise you get xml, not sure if that’s the case with Mongodb.

Another possibility is your json response is a text string and you just need to parse it to json in which case jsonlibrary might be useful.

Dave.

Hi Dave,

thanks for your help,
I’m using the following Libraries:

i have this test using MongoDBLibrary - i get the output but not as a json.

i tried several option: to convert to string / json etc…
but nothing helped.

will appreciate your assistance on this issue (i’m a newbie in robot framework) -
in python code i was able to get it working by using:

results = collection.find({“name”: “moria.zvieli”})
l = list(results)
test = json.dumps(l, default=json_util.default)
print(test)

(not sure how to implement it on robot framework)

Hi Moshe,

Well using the RPA.JSON Library the equivalent of test = json.dumps(l, default=json_util.default) would probably be Convert String to JSON so I would suggest you try something like:

&{r_json}=     Convert string to JSON    ${results}
Log     ${r_json[0]['_id']}

Because that result is a list of dictionaries, I’m not sure, but you might need to do it like this:

@{r_json}=     Convert string to JSON    ${results}
Log     ${r_json[0]['_id']}

While i’ve never used RPA.JSON, I always try to use the libraries that are already in use first before adding new ones. Hopefully I’ve read the doco correctly.

Dave.

Hi Dave,

I tried your solution but still i get an error -

it seems that convert to Json failed.

any idea how get it as Json format?

thanks again

Hi Moshe,

Ah ha I see the problem, your result string from Mongodb is not a valid JSON string (maybe it’s not a string at all?)

A couple of things to try:

  1. lets see if it’s already a dictionary like object?
Log     ${results[0]}
Log     ${results[0]["_id"]}
  1. if that fails then lets try replacing the ’ with " with Replace String (you may need to add Library String
${resultsdq} =	Replace String	${results}	'	"
&{r_json}=     Convert string to JSON    ${resultsdq}
Log     ${r_json[0]}

Hopefully one of these will work,

Dave.

Hi Dave,
I will try your suggestion.
In my privious debugging, I saw while testing that it returns as a list so I tried to convert the list to json but without success.

I will update soon regarding your suggestion. Thanks again

Hi Dave,

i tested your suggestion:

still got an error. how can we convert list to valid json?
thanks for your assistance

Hi Moshe,

Ignore the error for a moment and look at what these lines have you:

So that gives you the _id first result, which is what I thought you were trying to achieve? (it’s what you pointed to in your second post)

Now we know that ${results} is a list of dictionaries. Knowing this we can now get any value you need from that result.

I guess my next question is what are you trying to achieve, do you really need to convert it to JSON? if so why? I ask because i’m trying to understand your next step after conversion to JSON?

Libraries like requests library that send and receive JSON are usually able to convert the dictionary to JSON for you, and if you are just trying to get or manipulate values from in the result you already can, so there may not be a need to go back to a JSON string.

Dave.

Hi Dave,
You are right, ${results} is a list of dictionaries.
my main goal is to use the JSONpath to retrieve/ manipulate values from the json.
I was able also to use a custom python code like:
from bson import json_util
import json

def convertlisttojson(mongo_result):
l = list(mongo_result)
mongo_json_result = json.dumps(l, default=json_util.default)
#print(mongo_json_result)
return mongo_json_result

thanks again for your assistance.
Another thing to mention about my other post regarding fetching latest document from MongoDB, I was not able to find proper way to get the the latest document from a collection. do you have an idea how it can be achieved?
in mongoDB documentation i looked at the sort options but could not implement it using the info in-
https://momenh.github.io/robotframework-mongodb-bson-library/MongoDBBSONLibrary.html
with ‘Retrieve Mongodb Records With Desired Fields’

Hi Moshe,

if I read the doco right, then you should be able to use json path on the ${results} variable directly Get value from JSON

${first_id}=     Get value from JSON      ${results}   $[0]._id
${second_id}=     Get value from JSON      ${results}   $[1]._id

But you should also be able to covert it to a JSON string first using Convert JSON to String

${json}=   Convert JSON to string    ${results}
${first_id}=     Get value from JSON      ${json}   $[0]._id
${second_id}=     Get value from JSON      ${json}   $[1]._id

Dave.

Hi Dave,

Thanks again ,

I meant if there is an option in robot framework to get the latest document in mongodb collection?

Moshe