Search for array value inside JSON column - laravel

When there is a single value, whereJsonContains does the job. But in this case, I have an array which is as a result of saving to JSON column being converted into string. Here is the example:
[
{
"key":"cUyV6kW3noxxW85G",
"value":"value-1",
},
{
"key":"R8dHf4vWBS8M4W5G",
"value":"value-2",
"multidimensional_array":{
"table":[
{
"attributes":{
"array":"[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\"]"
}
}
],
}
},
{
"key":"cUyV6kW3noxxW85G",
"value":"value-3",
},
]
I want to get those elements which contain specific value inside array. Here is my try, but it's not working, no results are being returned:
ExampleTable::whereJsonContains('example_column', ['multidimensional_array' => ['table' => ['attributes' => ['array' => $specific_value]]]])->get();
Is it even possible to do it this way, or do I need to get every single result which has multidimensional_array and json_decode() array so I can iterate over it to look for what I need?

take a look at the documentation: https://laravel.com/docs/master/queries#json-where-clauses
You should use the whereJsonContains a bit different;
ExampleTable::whereJsonContains('multidimensional_array->table->attributes->array', $specific_value)->get();

Related

Match keys with sibling object JSONATA

I have an JSON object with the structure below. When looping over key_two I want to create a new object that I will return. The returned object should contain a title with the value from key_one's name where the id of key_one matches the current looped over node from key_two.
Both objects contain other keys that also will be included but the first step I can't figure out is how to grab data from a sibling object while looping and match it to the current value.
{
"key_one": [
{
"name": "some_cool_title",
"id": "value_one",
...
}
],
"key_two": [
{
"node": "value_one",
...
}
],
}
This is a good example of a 'join' operation (in SQL terms). JSONata supports this in a path expression. See https://docs.jsonata.org/path-operators#-context-variable-binding
So in your example, you could write:
key_one#$k1.key_two[node = $k1.id].{
"title": $k1.name
}
You can then add extra fields into the resulting object by referencing items from either of the original objects. E.g.:
key_one#$k1.key_two[node = $k1.id].{
"title": $k1.name,
"other_one": $k1.other_data,
"other_two": other_data
}
See https://try.jsonata.org/--2aRZvSL
I seem to have found a solution for this.
[key_two].$filter($$.key_one, function($v, $k){
$v.id = node
}).{"title": name ? name : id}
Gives:
[
{
"title": "value_one"
},
{
"title": "value_two"
},
{
"title": "value_three"
}
]
Leaving this here if someone have a similar issue in the future.

How to save a specific array element in Logstash

I am receiving a JSON object with an array property. I would like to search the array and save only the element that matches my criteria. My input looks like this:
{
"identifier": [
{ "system" : "Source1", "value" : "TheValueIDontWant"},
{ "system" : "Source2", "value" : "TheValueIWant"}
]
}
and I would like my output to look like this:
{
"SourceID": "TheValueIWant"
}
So in this case, I want to search the identifier array for the element which has Source2 as the system and save its corresponding value to my new property.
Is there a way to do this in Logstash?
Thanks
Got this answer from someone on the elastic forum. Using ruby was indeed the answer and this is how:
ruby {
code => '
ids = event.get("identifier")
if ids.is_a? Array
ids.each { |x|
if x["system"] == "Source2"
event.set("SourceID", x["value"])
end
}
end
'
}

How to correlate drop down list

I have a response like below-
"distributionChannelList":[
{
"id":1,
"description":"Agency1"
},
{
"id":5,
"description":"Agency2"
},
{
"id":4,
"description":"Agency3"
},
{
"id":3,
"description":"Agency4"
}
],
"marketingTypeList":[
{
"id":1,
"description":"Type1".......
There are so many 'id' and 'description' values in my response. Agency1, Agency2.. are drop downs in my application.
So I want Jmeter to pick a different agency every time and pass in subsequent requests.
How to achieve this?
Use json extractor or reg Ex to fetch all the description with Match No. as 0 for random. Pass the Json created variable to the next request like ${varDescription}. On every run, random value will be fetched and provided to the next request.
Below snapshot is an example for regex but prefer json in your case. For fetching with json use $..description as json path expression. Repeat the same for others if required.
Hope this helps.
Update:-
Please check the below config. It will extractor 2 values in sync. But, ${cnt} should be same value. I have used counter just for demo. You can use random function to generate value between 1 to 4 and pass that variable ${rnd};${rnd}.

Query unknown data structure in GraphQL

I just started to work with GraphQL and I am setting up a server with webonyx/graphql-php at the moment. Since a GraphQL query already has to contain the resulting data structure, I am not quite sure how to get dynamic data. Assumed that I query the content which consists different element types and my final structure should look like this:
{
"data": {
"dataset": {
"uuid": "abc...",
"insertDate": "2018-05-04T12:12:12Z",
// other metadata
"content": [
{
"type": "headline",
"text": "I am a headline"
},
{
"type": "image",
"src": "http://...",
"alt": "I am an image"
},
{
"type": "review",
"rating": 3,
"comment": "I am a review"
},
{
"type": "headline",
"text": "I am another headline"
}
// other content elements
]
}
}
}
How could I write a query for this example?
{
dataset {
uuid
insertDate
content {
????
}
}
}
And how would a type definition for the content section look like? There is a defined set of element types (headline, image, review, many more) but their order and number of elements is unknown and they have only one field, type, in common. While writing the query in my frontend, I don't know anything about the content structure. And what would the graphql-php type definition for the content section look like? I couldn't find any similar example online, so I am not sure if it is even possible to use GraphQL for this use case. As an extra information, I always want to query the whole content section, not a single element or field, always everything.
When you're returning an array of Object types, but each individual item could be one of any number of different Object types, you can use either an Interface or a Union. We can use an Interface here since all the implementing types share a field (type).
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\Type;
$content = new InterfaceType([
'name' => 'Content',
'description' => 'Available content',
'fields' => [
'type' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The type of content',
]
],
'resolveType' => function ($value) {
if ($value->type === 'headline') {
return MyTypes::headline();
} elseif ($value->type === 'image') {
return MyTypes::image();
} # and so on
}
]);
Types that implement the Interface need to do so explicitly in their definition:
$headline = new ObjectType([
# other properties
'interfaces' => [
$content
]
]);
Now if you change the type of the content field to a List of content, you can query only fields specific to each implementing type by using inline fragments:
query GetDataset {
dataset {
uuid
insertDate
content {
type # this field is shared, so it doesn't need an inline fragment
... on Headline {
text
}
... on Image {
src
alt
}
# and so on
}
}
}
Please see the docs for more details.

RethinkDB - Filter using Match() by value in same dataset and table

So, since I'm too dumb obviously to figure this out myself, I'll ask you better folks here on SO instead.
Basically i have a datastructure that looks like the following:
....,
{
"id": 12345
....
"policy_subjects": [
{
"compiled": "^(user|max|anonymous)$",
"template": "<user|max|anonymous>"
},
{
"compiled": "^max$",
"template": "max"
}
]
....
}
compiled is a "compiled" regex
template is the same regex without regex-modifiers
What I want is to do a simple query in RethinkDB using the "compiled" value and matching that against a string, say "max".
Basically
r.table("regex_policies").filter(function(policy_row) {
return "max".match("(?i)"+policy_row("policy_subjects")("compiled"))
}
Is what i want to do (+case-insensitive search)
There are of course lots of policy_subjects in the database so in this example the result should be the whole dataset (1 result) that matches "max". Since "max" exists twice in this case and it matches both regexes (once would have been enough).
"foobar" would likewise in this example yield 0 results, since any of the compiled regexes does not match "foobar".
Does anyone know how to do this relatively simple query?
You definitely want to use r.expr here and I got this example to work:
r.expr([{
"id": 12345,
"policy_subjects": [
{
compiled: "^(user|max|anonymous)$",
template: "<user|max|anonymous>"
},
{
compiled: "^max$",
template: "max"
}
]
}]).merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("max").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
Changing max to something else that does not match, returns the document with no elements inside policy_subjects.
For example, changing max => to wat (my favorite test string of all time) looks like this:
.merge(function(policy_row) {
return {
"policy_subjects": policy_row("policy_subjects").filter(function(item){
return r.expr("wat").match(r.expr("(?i)").add(item("compiled"))).ne(null);
})
}
})
And results in this:
[
{
"id": 12345 ,
"policy_subjects": [ ]
}
]
I think your logic for reducing to the one policy_subject document you want might be a little subjective to your use case so I'm not sure what the right answer is but you can use .reduce(...) to just return the right-most value.

Resources