How to get ArrayObject using document

Hi, I have a document with below json format. how i can get “Price” object to update “RetailPrice”

I tried below code but no luck.
var priceObj = database.GetDocument(id).ToMutable().GetDictionary(“ProductList.Price”);
var priceObj = database.GetDocument(id).ToMutable().GetArray(“ProductList[0].Price”);

Product.Json

{
 "Id": "doc_sales_06172020",
    "$DocVrsn": "1.0",
    "$Type": "sales",
    "$MdfdById": "user",
    "$MdfdTmstmp": "2020-17-06T22:06:43.000Z",
	"ProductList": [
        {
		"ProductName": "p1",
		"Price"
			{
			"RetailPrice": 1.50,
			"WholesalePrice" : 1.20
			}
		},
		{
		"ProductName": "p2",
		"Price"
			{
			"RetailPrice": 2.50,
			"WholesalePrice" : 2.20
			}
		},
		{
		"ProductName": "p3",
		"Price"
			{
			"RetailPrice": 3.50,
			"WholesalePrice" : 3.20
			}
		}
		]
}

Hi @GPK,

Which version of the Couchbase .NET SDK are you using?

Couchbase.Lite 2.7.1.0 and.net framework 4.7.2

Okay, that’s why it looked a little different to me :slight_smile:

I’m tagging @borrrden as someone who can help with Xamarin, and I’m moving this question over to the Mobile forum.

1 Like

@Sandy_Chuang is the one to contact about .NET :slight_smile:

Hi @Sandy_Chuang any help on this much appreciate. Thanks

Hi @GPK
Can you show me how you construct the Couchbase Lite document?

Thanks @Sandy_Chuang for help. Below is detail how i construct the doc.

        var price1 = new Price
                         {
                             RetailPrice = (decimal)1.50,
                             WholesalePrice = (decimal)2.50
                         };

        var price2 = new Price
                         {
                             RetailPrice = (decimal)3.50,
                             WholesalePrice = (decimal)4.50
                         };

        var priceList = new List<Price>
                            {
                                price1,
                                price2
                            };

        var product = new ProductList
                          {
                              ProductName = "sales",
                              Price = priceList
                          };

        var products = new List<ProductList>
                           {
                               product
                           };

        var testClass = new TestClass()
                            {
                                Id = "doc_sales_061720201",
                                DocVrsn = "1.0",
                                Type = "sales",
                                ProductList = products
                            };

        var map = JsonConvert.DeserializeObject<IDictionary<string, object>>(testClass.ToString());

        var doc = new MutableDocument(testClass.Id, map);
        database.Save(doc);

        var docObject = database.GetDocument("doc_sales_06172020").ToMutable();

        var retailPrice = docObject.GetDictionary("ProductList.Price.RetailPrice"); //return null
        var retailPrice1 = docObject.GetDictionary("Price.RetailPrice");//return null
        var retailPrice2 = docObject.GetArray("Price[0].RetailPrice"); //return null

@GPK
Couchbase Lite Document is a dictionary object. The dictionary value type can only be byte, sbyte, short, ushort, int, uint, long, ulong, float, double, bool, DateTimeOffset, Blob, a one-dimensional array or a dictionary whose members are one of the preceding types.

So in the cbl doc construction example you just provided, the doc should look something like

var p = new Dictionary<string, object> {
                ["id"] = testClass.Id,
                ["DocVrsn"] = testClass.DocVrsn,
                ["Type"] = testClass.Type,
                ["ProductList"] = new Dictionary<string, object> {
                    ["ProductName"] = "sales",
                    ["Price List"] = new List<Dictionary<string, object>>() {
                        new Dictionary<string, object> {
                            ["RetailPrice"] = 1.50,
                            ["WholesalePrice"] = 2.5
                        },
                        new Dictionary<string, object> {
                            ["RetailPrice"] = 3.50,
                            ["WholesalePrice"] = 4.5
                        }
                    }
                }
            };

var doc = new MutableDocument(testClass.Id, p);
Db.Save(doc);

then you can access ProductList like

var productList = Db.GetDocument("doc_sales_061720201").GetDictionary("ProductList");

Thanks @Sandy_Chuang for the help