Should be a no brainer, but I'm can't seem to access the elements returned from Newtonsoft's json deserializer.
Example json:
{
"ns0:Test": {
"xmlns:ns0": "http:/someurl",
"RecordCount": "6",
"Record": [{
"aaa": "1",
"bbb": "2",
},
{
"aaa": "1",
"bbb": "2",
}]
}
}
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(somestring);
Stripping out the json up to the Record text, i can access the data without issue.
i.e. result.Recordcount
If i leave the json as shown above, can someone enlighten me how to access Recordcount?
All inputs appreciated. Thanks!
For those JSON properties that have punctuation characters or spaces (such that they cannot be made into valid C# property names), you can use square bracket syntax to access them.
Try this:
int count = result["ns0:Test"].RecordCount;
Related
{
"metadata": {
"id": "2",
"uri": "3",
"type": "2"
},
"Number": "2323600002913",
"Date": "04/21/2009",
"postingDate": "00/00/0000",
"ata": {
"results": [
{
"metadata": {
"id": "r",
"uri": "e2",
"type": "s2"
},
"item": "000010",
"data":"ad"
}
]
}
}
want to remove metadata property from above json message and output should be like below
{
"Number": "2323600002913",
"Date": "04/21/2009",
"postingDate": "00/00/0000",
"ata": {
"results": [
{
"item": "000010",
"data":"ad"
}
]
}
}
I tried with removeProperty() which is working for root level metadata but inside metadata not removed.
how to use replace() in this case or anything else to only remove metadata.
The simplest way is use inline code, cause even with removeProperty() expression to remove the metadata under results, it will return the results array data not the whole json data. Then you will have to combine them, it's not a convenient way.
And with inline code you could refer to my below picture. The variable json is the value from triggerbody, then just delete the node or key and return the json variable. And with this way, even you want to delete many metadata in the array, you could add a for loop to delete it, just think of it as plain js code.
Update:if you want to get value from variable,cause no support expression to get value from variable so use the below expression.
var json =wworkflowContext.actions.Initialize_variable.inputs.variables[0].value;
And about how to loop the array in the json refer to my below pic.
I am trying to get json but am not sure how to read it. Here is the ouput of the json. Is quoteResponse an array or an object? also result is an array or an object? Thanks.
{
"quoteResponse": {
"result": [{
"language": "en-US",
"region": "US",
"quoteType": "EQUITY",
"quoteSourceName": "Delayed Quote",
"sourceInterval": 15,
"exchangeTimezoneName": "America/New_York",
"exchangeTimezoneShortName": "EDT",
"pageViews": {
"shortTermTrend": "UP",
"midTermTrend": "UP",
"longTermTrend": "UP"
},
"gmtOffSetMilliseconds": -14400000,
"marketState": "CLOSED",
"market": "us_market",
"priceHint": 2,
"esgPopulated": false,
"tradeable": true,
"triggerable": true,
"exchange": "NYQ",
"regularMarketPrice": 47.23,
"postMarketChangePercent": 0.0,
"postMarketTime": 1568407854,
"postMarketPrice": 47.23,
"postMarketChange": 0.0,
"regularMarketChangePercent": 0.66070205,
"exchangeDataDelayedBy": 0,
"regularMarketTime": 1568404920,
"regularMarketChange": 0.31000137,
"regularMarketVolume": 295978,
"regularMarketPreviousClose": 46.92,
"fullExchangeName": "NYSE",
"longName": "Cabot Corporation",
"shortName": "Cabot Corporation",
"symbol": "CBT"
}],
"error": null
}
}
Your quoteResponse is an object as
JSON objects are surrounded by curly braces {}.
They are written in key/value pairs.
Extract from your original JSON to have it directly in my answer for the purpose of comparison.
{
"quoteResponse": {
"result": [{
"language": "en-US"
"pageViews": {
"shortTermTrend": "UP"
}]
}
}
The result is just an array.
A bit offtopic, but maybe interesting:
I landed on a page which describe Javascript object literals and those are pretty similar to JSON but not the same. JSON derives from Javascript object literal syntax, so the syntax of both are very similar, one directly noticeable difference is that all names in JSON must be wrapped in double quotes.
JavaScript objects can also contain code (functions) and references to other objects. Their keys can be strings, numbers and symbols. etc.
This is an object which is shown by {}.
An array is contained in square brackets [].
You can also have an array of objects [{},{}] - just like the result in your example. You can keep on nesting.
quoteResponse is an object, its value is contained within braces {...}.
result is an array, it is contained within brackets [...].
quoteResponse is an object. It has properties result and error. In json object has following format {property: value, property2: value}
result is an array in json arrays has following format array: [{}, {}...]
quoteResponse is a JSONObject and result is a JSONArray. A JSONObject can be identified by the enclosing curly braces
{"quoteResponse":{"result":[{"language":"en-US","region":"US","symbol":"CBT"}],"error":null}}
Whereas a JSONArray can be identified by the enclosing box brackets as in result.
"result":[{"language":"en-US","region":"US","regularMarketChange":0.31000137,"regularMarketVolume":295978,"regularMarketPreviousClose":46.92,"fullExchangeName":"NYSE","longName":"Cabot Corporation","shortName":"Cabot Corporation","symbol":"CBT"}]
I have following flow in NIFI , JSON has (1000+) objects in it.
invokeHTTP->SPLIT JSON->putMongo
Flow works fine, till I receive some keys in json with "." in the name. e.g. "spark.databricks.acl.dfAclsEnabled".
my current solution is not optimal, I have jotted down bad keys, and using multiple replace text processor to replace "." with "_". I am not using REGEX, I am using string literal find/replace. So each time I am getting failure in putMongo processor, I am inserting new replaceText processor.
This is not maintainable. I am wondering if I can use JOLT for this? couple of info regarding input JSON.
1) no set structure, only thing that is confirmed is. everything will be in events array. But event object itself is free form.
2) maximum list size = 1000.
3) 3rd party JSON, so I cant ask for change in format.
Also, key with ".", can appear anywhere. So I am looking for JOLT spec that can cleanse at all level and then rename it.
{
"events": [
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1531896847915,
"type": "EDITED",
"details": {
"previous_attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled": "true",
"spark.databricks.repl.allowedLanguages": "python,sql"
},
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"previous_cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"user": ""
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535540053785,
"type": "TERMINATING",
"details": {
"reason": {
"code": "INACTIVITY",
"parameters": {
"inactivity_duration_min": "15"
}
}
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535537117300,
"type": "EXPANDED_DISK",
"details": {
"previous_disk_size": 29454626816,
"disk_size": 136828809216,
"free_space": 17151311872,
"instance_id": "6cea5c332af94d7f85aff23e5d8cea37"
}
}
]
}
I created a template using ReplaceText and RouteOnContent to perform this task. The loop is required because the regex only replaces the first . in the JSON key on each pass. You might be able to refine this to perform all substitutions in a single pass, but after fuzzing the regex with the look-ahead and look-behind groups for a few minutes, re-routing was faster. I verified this works with the JSON you provided, and also JSON with the keys and values on different lines (: on either):
...
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled":
"true",
"spark.databricks.repl.allowedLanguages"
: "python,sql"
},
...
You could also use an ExecuteScript processor with Groovy to ingest the JSON, quickly filter all JSON keys that contain ., perform a collect operation to do the replacement, and re-insert the keys in the JSON data if you want a single processor to do this in a single pass.
I am thinking about using JSON as a way to communicate information inside my programm in a way suggested in this talk.
As i read the JavaScript Object Notation i see no way of noting my objecttype.
Suppose i communicate the string { "val" : 5 }, how would i know what that string was for.
I would like to send the string error = { "val" : 5 } and measurement = { "val" : 5 }. but as i read it this would not be valid JSON notation.
Is the solution always something like { "type" : "error", "val" : 5 } or am i missing some bigger concept in JavaScript Object Notation.
EDIT: ops - did not do JSON in my examples, fixed that
{
"type": "error",
"val": 5
}
That's the proper way to format your JSON
If you have different type of values, then you will be able to have an array looking like this:
[
{
"type": "error",
"val": 5
},
{
"type": "measurement",
"val": 45
}
]
In JSON (and Javascript in general), the key name identifies the type of the value. A JSON-like version of your examples:
{
'error': 5,
'measurement': 5
}
How does bind values of embedded JSON data to input elements in EmberJS as I cannot seem to find a straight forward way to get that done.
update
It doesn't seem to work for a JSON object with this structure
{
"users": [
{
"_id": "534550428047526419000002",
"Name": "admin",
"Code": "admin",
"Age": 12,
"Sex": "Male",
"Ethnicity": "admin",
"EventTime": "",
"Accident": [
{
"value": true,
"risk": "Medium"
}
]
}
]
}
In the template you use the property name
{{foo}}
{{input value=foo}}
Example
http://emberjs.jsbin.com/wufegaca/1/edit
nested properties are handled the same way they are in most languages with the dot operator
{{foo.bar}}
{{input value=foo.bar}}
Example
http://emberjs.jsbin.com/wufegaca/2/edit
Using an array you need to iterate over the array. If you didn't how would Ember know which item you were referring to? Additionally upper case property names are problematic in handlebars, since it usually denotes a global namespace, so be wary if things don't appear to be working.
Example
http://emberjs.jsbin.com/wufegaca/3/edit