How to fetch #value from following json - asp.net

I have following json.I want to fetch "rdfs:label"'s value when "#type" is "T:Class".
I want output like
Stand1
Stand2
How to achieved this.
I have tried like this.
string json = File.ReadAllText("Filepath");
JObject Search = JObject.Parse(json);
IList results = Search["#standard"][0].Children().ToList();
foreach (JToken result in results)
{
}
It gives me entire inner #standard block.
{
"#School": {
"name": "Vikas Mandir",
"subject": "Maths",
"author": "Joshi",
},
"#standard": [
{
"#id": "vikas:Stand1",
"#standard": [
{
"#id": "vikas:Stand12",
"#type": "T:Class",
"tag:Std:label": {
"#value": "Stand1"
},
"rdfs:label": {
"#value": "Stand1"
}
},
{
"#id": "vikas:Stand123",
"#type": "T:Class",
"tag:Std:label": {
"#value": "Stand2"
},
"rdfs:label": {
"#value": "Stand2"
}
}
]
}
]
}

try this
var prop = new List<JToken>();
GetObject(jsonParsed, prop, "rdfs:label", null, true);
List<string> found = prop.Where(i => (string)i["#type"] == "T:Class")
.Select(i => (string) ((JObject)i).Properties().First(x => x.Name == "rdfs:label").Value["#value"]).ToList();
Console.WriteLine(string.Join(",",found)); //Stand1,Stand2
helper
public void GetObject(JToken obj, List<JToken> result, string name = null, string value = null, bool getParent = false)
{
if (obj.GetType().Name == "JObject")
foreach (var property in ((JObject)obj).Properties())
{
if (property.Value.GetType().Name == "JValue"
&& (name == null || (string)property.Name == name)
&& (value == null || (string)property.Value == value))
{
if (getParent) result.Add(property.Parent); else result.Add(property);
}
else if (property.Value.GetType().Name == "JObject")
{
if (name != null && (string)property.Name == name) result.Add(property.Parent);
GetObject(property.Value, result, name, value);
}
else if (property.Value.GetType().Name == "JArray") GetObject(property.Value, result, name, value);
}
if (obj.GetType().Name == "JArray")
{
foreach (var property in ((JArray)obj))
{
if (property.GetType().Name == "JObject") GetObject(property, result, name, value);
if (property.GetType().Name == "JArray") GetObject(property, result, name, value);
if (property.GetType().Name == "JValue"
&& (value == null || (string)property == value))
{
if (getParent) result.Add((JArray)obj); else result.Add(property);
}
}
}
}

Related

aws glue job to import dynamodb data

We are trying to do DynamoDB migration from prod account to stage account.
In the source account, we are making use of "Export" feature of DDB to put the compressed .json.gz files into destination S3 bucket.
We have written a glue script which will read the exported .json.gz files and writes it to DDB table.
We are making the code generic, so we should be able to migrate any DDB table from prod to stage account.
As part of that process, while testing we are facing issues when we are trying to write a NUMBER SET data to target DDB table.
Following is the sample snippet which is raising ValidationException when trying to insert into DDB
from decimal import Decimal
def number_set(datavalue):
# datavalue will be ['0', '1']
set_of_values = set()
for value in datavalue:
set_of_values.add(Decimal(value))
return set_of_values
When running the code, we are getting following ValidationException
An error occurred while calling o82.pyWriteDynamicFrame. Supplied AttributeValue is empty, must contain exactly one of the supported datatypes (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: UKEU70T0BLIKN0K2OL4RU56TGVVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
However, if instead of Decimal(value) if we use int(value) then no ValidationException is being thrown and the job succeeds.
I feel that write_dynamic_frame_from_options will try to infer schema based on the values the element contains, if the element has "int" values then the datatype would be "NS", but if the element contains all "Decimal type" values, then it is not able to infer the datatype.
The glue job we have written is
dyf = glue_context.create_dynamic_frame_from_options(
connection_type="s3",
connection_options={
"paths": [file_path]
},
format="json",
transformation_ctx = "dyf",
recurse = True,
)
def number_set(datavalue):
list_of_values = []
for value in datavalue:
list_of_values.append(Decimal(value))
print("list of values ")
print(list_of_values)
return set(list_of_values)
def parse_list(datavalue):
list_of_values = []
for object in datavalue:
list_of_values.append(generic_conversion(object))
return list_of_values
def generic_conversion(value_dict):
for datatype,datavalue in value_dict.items():
if datatype == 'N':
value = Decimal(datavalue)
elif datatype == 'S':
value = datavalue
elif datatype == 'NS':
value = number_set(datavalue)
elif datatype == 'BOOL':
value = datavalue
elif datatype == 'M':
value = construct_map(datavalue)
elif datatype == 'B':
value = datavalue.encode('ascii')
elif datatype == 'L':
value = parse_list(datavalue)
return value
def construct_map(row_dict):
ddb_row = {}
for key,value_dict in row_dict.items():
# value is a dict with key as N or S
# if N then use Decimal type
ddb_row[key] = generic_conversion(value_dict)
return ddb_row
def map_function(rec):
row_dict = rec["Item"]
return construct_map(row_dict)
mapped_dyF = Map.apply(frame = dyf, f = map_function, transformation_ctx = "mapped_dyF")
datasink2 = glue_context.write_dynamic_frame_from_options(
frame=mapped_dyF,
connection_type="dynamodb",
connection_options={
"dynamodb.region": "us-east-1",
"dynamodb.output.tableName": destination_table,
"dynamodb.throughput.write.percent": "0.5"
},
transformation_ctx = "datasink2"
)
can anyone help us in how can we unblock from this situation?
Record that we are trying to insert
{
"region": {
"S": "to_delete"
},
"date": {
"N": "20210916"
},
"number_set": {
"NS": [
"0",
"1"
]
},
"test": {
"BOOL": false
},
"map": {
"M": {
"test": {
"S": "value"
},
"test2": {
"S": "value"
},
"nestedmap": {
"M": {
"key": {
"S": "value"
},
"nestedmap1": {
"M": {
"key1": {
"N": "0"
}
}
}
}
}
}
},
"binary": {
"B": "QUFBY2Q="
},
"list": {
"L": [
{
"S": "abc"
},
{
"S": "def"
},
{
"N": "123"
},
{
"M": {
"key2": {
"S": "value2"
},
"nestedmaplist": {
"M": {
"key3": {
"S": "value3"
}
}
}
}
}
]
}
}

FOS Elastic range price with discount

I want to get all the products by range price with discount.
this is what it looks like in sql:
WHERE (
CASE WHEN p.discount IS NOT NULL THEN ROUND(
p.unit_price * (100 - p.discount) / 100, 1)
ELSE p0_.unit_price END ) >= :min
AND (
CASE WHEN p.discount IS NOT NULL THEN ROUND(
p.unit_price * (100 - p.discount) / 100, 1)
ELSE p0_.unit_price END ) <= :max
is there a way to do the same with range condition?
$fieldRange = new \Elastica\Query\Range('unitPrice', array('gte' => 300, 'lte' => 1500));
here is my config:
fos_elastica:
clients:
default: { url: '%env(ELASTICSEARCH_URL)%' }
indexes:
product:
properties:
unitPrice:
type: integer
discount:
type: keyword
attributeValues:
type: "nested"
properties:
value:
type: keyword
product:
type: keyword
persistence:
driver: orm
model: App\Entity\Product
provider: ~
listener: ~
finder: ~
here is the full query:
$query = new \Elastica\Query();
$query->setSize(0);
$boolQuery = new \Elastica\Query\BoolQuery();
/* filter checked */
$fieldQuery = new \Elastica\Query\Match();
$fieldQuery->setFieldQuery('attributeValues.value', 'Brand');
$domainQuery = new \Elastica\Query\Nested();
$domainQuery->setPath('attributeValues');
$domainQuery->setQuery($fieldQuery);
$fieldQuery2 = new \Elastica\Query\Match();
$fieldQuery2->setFieldQuery('attributeValues.value', 'Another Brand');
$domainQuery2 = new \Elastica\Query\Nested();
$domainQuery2->setPath('attributeValues');
$domainQuery2->setQuery($fieldQuery2);
$fieldRange = new \Elastica\Query\Range('unitPrice', array('gte' => 300, 'lte' => 1500));
$boolQuery->addMust($domainQuery);
$boolQuery->addMust($domainQuery2);
$boolQuery->addMust($fieldRange);
$query->setQuery($boolQuery);
$agg = new \Elastica\Aggregation\Nested('attributeValues', 'attributeValues');
$names = new \Elastica\Aggregation\Terms('value');
$cardinality = new \Elastica\Aggregation\Cardinality('unique_products');
$cardinality->setField('attributeValues.product');
$names->setField('attributeValues.value');
$names->setSize(100);
$names->addAggregation($cardinality);
$agg->addAggregation($names);
$query->addAggregation($agg);
$companies = $this->finder->findPaginated($query);
$asd = $companies->getAdapter()->getAggregations();
here is the result:
array(
"attributeValues" => array:2(
"doc_count" => 406,
"value" => array:3(
"doc_count_error_upper_bound" => 0,
"sum_other_doc_count" => 0,
"buckets" => array:42(
2 => array:3(
"key" => "Another Brand",
"doc_count" => 15,
"unique_products" => array:1(
"value" => 9
)
)
)
)
)
);
here is native request (just in case):
{
"size": 0,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "attributeValues",
"query": {
"match": {
"attributeValues.value": {
"query": "Brand"
}
}
}
}
},
{
"nested": {
"path": "attributeValues",
"query": {
"match": {
"attributeValues.value": {
"query": "Another Brand"
}
}
}
}
},
{
"range": {
"unitPrice": {
"gte": 300,
"lte": 1500
}
}
}
]
}
},
"aggs": {
"attributeValues": {
"nested": {
"path": "attributeValues"
},
"aggs": {
"value": {
"terms": {
"field": "attributeValues.value",
"size": 100
},
"aggs": {
"unique_products": {
"cardinality": {
"field": "attributeValues.product"
}
}
}
}
}
}
}
}
a little explanation - I am making a smart filter that disables options when there are no products in it, which I calculate with this query. But I don't know how to calculate the price range with a discount (%) in elastica. I show how I do it in sql.

How to alias array elements in CosmosDB documents?

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:

How to get gmail attachment into local device store with intent filter

In my application i want to open gmail attachment with my application, i used intent filter for that,
I am getting URI and i want to use that and copy that attachment to my local device and want to display that with my application .
url = {content://com.google.android.gm.sapi/abc#gmail.com/message_attachment_external/%23thread-f%3A1610966348228029097/%23msg-f%3A1610966348228029097/0.1?account_type=com.google&mimeType=application%2Foctet-stream&rendition=1}
After a days of research and implementation, Here is a final solution for gmail attachment file , to view and down load in our application.
[IntentFilter(new string[] { Intent.ActionView }, Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = "content", DataMimeType = "application/octet-stream")]
Stream inputsTream = resolver.OpenInputStream(url);
fs = File.Create(fileName);
byte[] buffer = new byte[32768];
int count;
int offset = 0;
while ((count = inputsTream.Read(buffer, offset, buffer.Length)) > 0)
{
fs.Write(buffer, 0, count);
}
fs.Close();
inputsTream.Close();
private fun deepLinkAcceptor(){
var ins: InputStream? = null
var os: FileOutputStream? = null
var fullPath: String? = null
var file: File;
try {
val action = intent.action
if (Intent.ACTION_VIEW != action) {
return
}
val uri = intent.data
val scheme = uri!!.scheme
var name: String? = null
if (scheme == "content") {
val cursor = contentResolver.query(
uri!!, arrayOf(
MediaStore.MediaColumns.DISPLAY_NAME
), null, null, null
)
cursor!!.moveToFirst()
val nameIndex = cursor!!.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
if (nameIndex >= 0) {
name = cursor!!.getString(nameIndex)
}
} else {
return
}
if (name == null) {
return
}
val n = name.lastIndexOf(".")
val fileName: String
val fileExt: String
if (n == -1) {
return
} else {
fileName = name.substring(0, n)
fileExt = name.substring(n)
}
// create full path to where the file is to go, including name/ext
fullPath = ""
val filenm = fileName + fileExt
file = File(cacheDir, filenm)
ins = contentResolver.openInputStream(uri!!)
os = FileOutputStream(file.getPath())
val buffer = ByteArray(4096)
var count: Int
while (ins!!.read(buffer).also { count = it } > 0) {
os!!.write(buffer, 0, count)
}
os!!.close()
ins!!.close()
println("===onfile path: " + file.getAbsolutePath())
println("===onFile name: " + file.readText())
} catch (e: Exception) {
if (ins != null) {
try {
ins.close()
} catch (e1: Exception) {
}
}
if (os != null) {
try {
os.close()
} catch (e1: Exception) {
}
}
if (fullPath != null) {
val f = File(fullPath)
f.delete()
}
}
}

redux-persist, save only first 10 items using Transform

I'm trying to save only 2 items when persisting through redux-persist .
I'm perplexed why the following code won't work..
(it seems to save more than 2 items)
const myTransform = createTransform(
(inboundState, key) => {
let { openforum_threads } = inboundState
if (!openforum_threads) {
return inboundState
}
let { allIds } = openforum_threads
let STORE_NUM = 2
let storeIds = allIds.slice(0, STORE_NUM)
console.log('saving', storeIds)
let { byId } = openforum_threads
let storeById = {}
storeIds.map((id) => {
storeById[id] = byId[id]
})
openforum_threads = {
...openforum_threads,
allIds: storeIds,
byId: storeById
}
return { ...inboundState, openforum_threads}
},
(outboundState, key) => {
// convert mySet to an Array.
return outboundState
}
)

Resources