Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

Save a value which is invalid json

4 replies [Last post]
  • Login or register to post comments
Thu, 10/20/2011 - 04:19
robtindall
Offline
Joined: 08/01/2011
Groups: None

I am surprised by the following behaviour can you confirm that this is correct. Using the following code:

	OperationFuture<Boolean> future = couchbaseClient.set("key", 0, "{\"x\": }");
	System.out.println(future.get().booleanValue());

This causes an invalid json value to be added to the database, but the spymemcached client returns a true, meaning the set was OK. The resulting object for key "key" is:
{
	"_id": "key",
	"_rev": "9-54d7a4c976bbaae69ce136f350855854",
	"$flags": 0,
	"$expiration": 0,
	"$att_reason": "invalid_json",
	"_attachments": {
		"value": {
			"content_type": "application/content-stream",
			"revpos": 1,
			"digest": "md5-vG4IxeQGAHgaQTMvMxAbSQ==",
			"length": 7,
			"stub": true
		}
	}
}

If this is correct should a code my application to check for objects with a $att_reason, or how do you handle this. The reason this arose is we had some json that was not being escaped properly and spent sometime trying to understand what was happening..

Top
  • Login or register to post comments
Thu, 10/20/2011 - 13:50
mikew
Offline
Joined: 03/14/2011
Groups:

You can't get access to the $att_reason through a get operation because JSON parameters with $ in front of them are only used by the server. What I recommend doing is having a function in your client program that checks to make sure that your JSON is formatted correctly. The other thing you can do is to write a map function that finds all keys that contain invalid JSON. That function looks like this:

function (doc) {
emit(doc.$att_reason, null);
}

And it will return to you a list of documents that contain invalid JSON.

Edit: Removed an incorrect statement

Let me know if you have any other questions.

Top
  • Login or register to post comments
Fri, 10/21/2011 - 00:34
robtindall
Offline
Joined: 08/01/2011
Groups: None

Thanks, I'll look into what you suggest.

But at a higher level, don't you think that the 'set' method should return an error if the json is invalid ?

Top
  • Login or register to post comments
Sat, 10/22/2011 - 19:10
mikew
Offline
Joined: 03/14/2011
Groups:

Two reason that come to mind are:

1. Allowing people to choose to use Couchbase as a key-value store or a document database
2. Maintaining compatibility with Membase.

I think the way you are using Couchbase it makes sense that an error should be returned. What might be a good idea is if we had a checkbox in the web ui that you could check to say something like "Validate JSON". If it was checked then Couchbase would return an error. If it is not then you would be able to store both JSON and none JSON values.

I have filed an issue here: http://www.couchbase.org/issues/browse/MB-4373

You can follow the discussion. If others like your idea then maybe we will get it added.

Top
  • Login or register to post comments
Sat, 10/22/2011 - 19:35
mikew
Offline
Joined: 03/14/2011
Groups:

Looks like we will be implementing something like this, but there is currently no time frame for when it will be finished.

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker