Related
When deserializing a string, the curly braces are duplicating and this is disturbing the reading of some fields. I've tried everything, but I can't serialize correctly, without duplicate curly braces.
I already tried to do it like this:
dynamic values = JsonConvert.DeserializeObject<dynamic>(storedEvent.Data);
The storedEvent.Data property is of type string and contains this information:
"{\"PaisId\":31,\"PaisDivisaoAdministrativaNivelRemovedEventList\":[{\"Id\":6,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":5,\"PaisDivisaoAdministrativaTipoNome\":\"Município\",\"PaisDivisaoAdministrativaTipoOrigemId\":5,\"Timestamp\":\"2022-11-24T20:16:15.6020289-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":6},{\"Id\":5,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":1,\"PaisDivisaoAdministrativaTipoNome\":\"Estado\",\"PaisDivisaoAdministrativaTipoOrigemId\":null,\"Timestamp\":\"2022-11-24T20:16:15.6580242-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":5}],\"Timestamp\":\"2022-11-24T20:16:16.1892039-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent\",\"AggregateId\":31}"
Result obtained (Duplicate curly braces):
{{
"PaisId": 31,
"PaisDivisaoAdministrativaNivelRemovedEventList": [
{
"Id": 6,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 5,
"PaisDivisaoAdministrativaTipoNome": "Município",
"PaisDivisaoAdministrativaTipoOrigemId": 5,
"Timestamp": "2022-11-24T20:16:15.6020289-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 6
},
{
"Id": 5,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 1,
"PaisDivisaoAdministrativaTipoNome": "Estado",
"PaisDivisaoAdministrativaTipoOrigemId": null,
"Timestamp": "2022-11-24T20:16:15.6580242-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 5
}
],
"Timestamp": "2022-11-24T20:16:16.1892039-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent",
"AggregateId": 31
}}
Expected:
{
"PaisId": 31,
"PaisDivisaoAdministrativaNivelRemovedEventList": [
{
"Id": 6,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 5,
"PaisDivisaoAdministrativaTipoNome": "Município",
"PaisDivisaoAdministrativaTipoOrigemId": 5,
"Timestamp": "2022-11-24T20:16:15.6020289-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 6
},
{
"Id": 5,
"PaisId": 31,
"PaisNomePtBr": "",
"PaisDivisaoAdministrativaTipoId": 1,
"PaisDivisaoAdministrativaTipoNome": "Estado",
"PaisDivisaoAdministrativaTipoOrigemId": null,
"Timestamp": "2022-11-24T20:16:15.6580242-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelRemovedEvent",
"AggregateId": 5
}
],
"Timestamp": "2022-11-24T20:16:16.1892039-03:00",
"MessageType": "PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent",
"AggregateId": 31
}
Does anyone know of a solution?
Using dynamic is not a good option to work with Json objects dynamically
For this, you can use Jobject as follows
using Newtonsoft.Json.Linq;
string jsonStr= "{\"PaisId\":31,\"PaisDivisaoAdministrativaNivelRemovedEventList\":[{\"Id\":6,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":5,\"PaisDivisaoAdministrativaTipoNome\":\"Município\",\"PaisDivisaoAdministrativaTipoOrigemId\":5,\"Timestamp\":\"2022-11-24T20:16:15.6020289-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":6},{\"Id\":5,\"PaisId\":31,\"PaisNomePtBr\":\"\",\"PaisDivisaoAdministrativaTipoId\":1,\"PaisDivisaoAdministrativaTipoNome\":\"Estado\",\"PaisDivisaoAdministrativaTipoOrigemId\":null,\"Timestamp\":\"2022-11-24T20:16:15.6580242-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelRemovedEvent\",\"AggregateId\":5}],\"Timestamp\":\"2022-11-24T20:16:16.1892039-03:00\",\"MessageType\":\"PaisDivisaoAdministrativaNivelHierarquiasRemovedEvent\",\"AggregateId\":31}";
var obj = JObject.Parse(jsonStr);
//access properties
var paisId = obj["PaisId"];
//Access to PaisDivisaoAdministrativaNivelRemovedEventList
var paisList = obj["PaisDivisaoAdministrativaNivelRemovedEventList"].ToList();
//search on PaisDivisaoAdministrativaNivelRemovedEventList
var result = paisList.FirstOrDefault(x => (int)x["Id"] == 6);
You have to fix the code that returns the double braces string. If you don't have access to this code, the only way to get rid of double braces is to use string function
storedEvent.Data=storedEvent.Data.Substring(1,Data.Length-2);
after this you can use your code if you like, but I don't like dynamic, IMHO it is better to use Parse and sqware brakets to get data.
I have the following document,
{
"VehicleDetailId": 1,
"VehicleDetail": [
{
"Id": 1,
"Make": "BMW"
},
{
"Id": 1,
"Model": "ABDS"
},
{
"Id": 1,
"Trim": "5.6L/ASMD"
},
{
"Id": 1,
"Year": 2008
}
]
}
I want to give aliases for the array elements, something like this,
{
"VehicleDetailId": 1,
"Type": "VehicleDetail",
"VehicleDetail": [
{
"MakeId": 1,
"MakeValue": "BMW"
},
{
"ModelId": 1,
"ModelValue": "ABDS"
},
{
"TrimId": 1,
"TrimValue": "5.6L/ASMD"
},
{
"YearId": 1,
"YearValue": 2008
}
]
}
The following query seems to work fine, but since Id is common for all, it is repeating every time.
SELECT c.vehicleDetailId, ARRAY(SELECT v.Id AS MakeId, v.Make AS MakeValue,
v.Id AS ModelId, v.Model AS ModelValue,
v.Id AS TrimId, v.Trim AS TrimValue,
v.Id AS YearId, v.Year AS YearValue
FROM v IN c.VehicleDetail) AS VehicleDetail
FROM c
How should I write the query so that the Id does not repeat every time, and I can fetch an element from a specific position?
You could use UDF to implement your needs.
Udf code:
function userDefinedFunction(array){
var returnArray = [];
for(var i=0;i<array.length;i++){
var obj = array[i];
var map = {};
if(obj.Make){
map["MakeId"]= obj.Id;
map["MakeValue"]= obj.Make;
}else if(obj.Model){
map["ModelId"]= obj.Id;
map["ModelValue"]= obj.Model;
}else if(obj.Trim){
map["TrimId"]= obj.Id;
map["TrimValue"]= obj.Trim;
}else if(obj.Year){
map["YearId"]= obj.Id;
map["YearValue"]= obj.Year;
}
returnArray.push(map);
}
return returnArray;
}
Sql:
SELECT c.VehicleDetailId,udf.test(c.VehicleDetail) AS VehicleDetail
FROM c
Output:
Suppose I have a document with the following structure,
{
"VehicleDetailId": 1,
"VehicleDetail": [
{
"Id": 1,
"Make": "BMW"
},
{
"Id": 1,
"Model": "ABDS"
},
{
"Id": 1,
"Trim": "5.6L/ASMD"
},
{
"Id": 1,
"Year": 2008
}
]
}
Now I want to retrieve an array element located at a specific position from VehicleDetail array like I want to retrieve the second element, i.e.,
{
"Id": 1,
"Model": "ABDS"
}
or the third,
{
"Id": 1,
"Trim": "5.6L/ASMD"
}
How should I write the query to achieve this?
Use the built-in ARRAY_SLICE function. This allows you to select part of an array.
Pass the array, starting position, number of elements to select.
SELECT ARRAY_SLICE(c.VehicleDetail, 1, 1) As SecondElement
FROM c
Output:
{
"SecondElement": [
{
"Id": 1,
"Model": "ABDS"
}
]
}
I have this current data structure on my firebase
{
"bible-operators": [{
"op-id": 0,
"op-name": "Anonymous",
"bcv": 101001001,
"op-version": 1,
"pass": "none",
"setting1": 1,
"setting2": 2,
"setting3": 3,
"setting4": 4,
"setting5": 5,
"setting6": 6,
"setting7": 7,
"bg": 1
}, ... {
"op-id": 4,
"op-name": "Test User 4",
"bcv": 101001001,
"op-version": 1,
"pass": "pass4",
"setting1": 1,
"setting2": 2,
"setting3": 3,
"setting4": 4,
"setting5": 5,
"setting6": 6,
"setting7": 7,
"bg": 1
}]
}
I tried to make test update using this function
function update(op_id, nuname, nuversion, nubcv, nupass) {
firebase.database().ref('operators/' + op_id).set({
"op-name": nuname,
"op-version": nuversion,
pass: nupass,
bcv : nubcv
});
}
My expected result is to update the provided new data but retain the others that has no updates. However, after calling update, all non provided data were deleted from the database. setting1-7 and bg.
How can I run the update without losing data that has no new data provided?
Thanks
The function firebase.database().ref('bible-operators/' + op_id).set() replace the document, you have to use update() instead of set().
try it:
firebase.database().ref('bible-operators/' + op_id).update({
"op-name": nuname,
"op-version": nuversion,
pass: nupass,
bcv : nubcv
});
Using R, I generate a list that contains certain unquoted elements. Please see at the bottom- it is invalid javascript code.
R code (does not work)
outq <- lapply (out, function (el){
el <- if( is.factor(el$ann) ){
el$ann <- apply(el$ann, 1, function(e){ e <- paste('"', e, '"', sep="") })
}
})
In the R language, How can I quote the members of the list$x$ann factor?
When I try to parse this JSON, json2.js fails.
Sample Data (Invalid JSON)
results = JSON.parse(
{
"result": {
"tot": {
"molal": [ 0.00071243, 0.00071243, 4, 4 ],
"ann": [ , , , ]
},
"desc": {
"val": [ 8.3486, 4, 0.8531, 4.0025, 0.99999, 0.00072541, 0.00071243, 100, -1.2983e-05, -0.00016223, 17, 111.02, 55.511 ],
"ann": [ Charge balance, Adjusted to redox eq, , , , , , , , , , , ]
},
"species": {
"molal": [ 55.508, 0.00029101, 2.3071e-09, 0.00042017, 0.00028731, 4.4532e-06, 4.9292e-07, 0.00069149, 1.0274e-05, 6.2142e-06, 4.9139e-12, 4, 0, 4.1166e-27, 4, 8.5144e-21 ],
"act": [ 0.8531, 0.00010921, 4.4812e-09, 1.4857e-06, 7.7889e-05, 4.4532e-06, 9.6777e-07, 0.00024834, 3.3916e-06, 0.00028204, 4.9139e-12, 2.2702, 0, 4.1166e-27, 3.7925, 1.8453e-20 ]
},
"master": {
"molal": [ 0.00071243, 0.00071243, 4, 8.2332e-27, 4, 1.7029e-20 ]
},
"pphases": {
"moles": 9.9993,
"delta": -0.00071243
},
"ListInfo": {
"n": 1,
"format": true
}
}
}
);
Is there a reason you cannot use the RJSON / RJSONIO packages for R ?
I have advanced a step forward using this R code:
outq <- lapply (out, function (el){el <- if( is.factor(el$ann) ){ el$ann <- lapply(el$ann, function(e){ e <- paste('"', e, '"', sep="") })} else {el}})