I have a column with JSON objects. I need to extract all values from this objects. The problem is the keys are not fixed and contains some ids, so I can't extract values by exact key value. Here is an example:
{
"1220202132188388": {
"id": "1220202132188388",
"date": "2019-04-03"
},
"482928839992": {
"id": "482928839992",
"date": "2019-04-06"
}
}
So I'd like to get an array:
[
{
"id": "1220202132188388",
"date": "2019-04-03"
},
{
"id": "482928839992",
"date": "2019-04-06"
}
]
Presto has limited JSONPath support and $.* doesn't work. Is there any workaround?
You can cast json to map and use map_values function.
select
map_values(cast(json_parse(c1) as map<varchar, json>))
from
(
values '{
"1220202132188388": {
"id": "1220202132188388",
"date": "2019-04-03"
},
"482928839992": {
"id": "482928839992",
"date": "2019-04-06"
}
}'
) t(c1)
[{"id":"1220202132188388","date":"2019-04-03"}, {"id":"482928839992","date":"2019-04-06"}]
Related
I am trying to extract specific field from json by filtering data based on it's value instead of Index.
For example my json looks like below
"AllData": [
{
"ID": "1",
"Value": "Value1"
},
{
"ID": "2",
"Value": "Value2"
},
{
"ID": "3",
"Value": "Value3"
},
{
"ID": "4",
"Value": "Value4"
},
{
"ID": "5",
"Value": "Value5"
}
]
}
I need to project section (id and value) where value = valueX. But valueX may not always at index X it can be at any other index also. So while projecting I can not use Index. I need to project based on value. I can use contains operator in my where clause which helps to filter the arrays (list of AllData array) as shown below
MyDataSet
| where parse_json(MyJson) contains("Value5")
| project MyJson[5].ID, MyJson[5].Value // this may give wrong result because Value5 can be at some other index
Any Suggestions will be helpful.
you can use mv-apply: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mv-applyoperator
let my_value = "Value3";
print d = dynamic({"AllData": [
{
"ID": "1",
"Value": "Value1"
},
{
"ID": "2",
"Value": "Value2"
},
{
"ID": "3",
"Value": "Value3"
},
{
"ID": "4",
"Value": "Value4"
},
{
"ID": "5",
"Value": "Value5"
}
]
})
| mv-apply d = d.AllData on (
project ID = d.ID, Value = d.Value
| where Value == my_value
)
ID
Value
3
Value3
Here is my mongoose model
var agentSchema = new Schema({
name: String,
debit: [{
date: Date,
amount: Number,
hint: String,
}],
})
I need to get the date from debit array and change it to dd:mm:yyyy format on the ejs view
I have tried several ways with projection by datetostring conversion but it is working for only mongoose object not for array of object.
You can make use of the $map pipeline operator inside the $project stage to
apply a condition to each and every element of an array.
db.collection.aggregate([
{
"$project": {
"name": 1,
"debit": {
"$map": {
"input": "$debit",
"as": "d",
"in": {
"date": {
"$dateToString": {
"date": "$$d.date",
"format": "%d:%m:%Y",
// "onNull": "" // If Required
}
},
"amount": "$$d.amount",
"hint": "$$d.hint",
}
}
}
}
}
])
This will provide the following output.
[
{
"_id": ObjectId("5a934e000102030405000000"),
"debit": [
{
"amount": 23535,
"date": "06:02:2021",
"hint": "StringHint"
},
{
"amount": 2355,
"date": "16:03:2021",
"hint": "StringHint1"
}
],
"name": "String"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"debit": [
{
"amount": 25,
"date": "22:06:2021",
"hint": "StringHint2"
},
{
"amount": 55,
"date": "01:07:2021",
"hint": "StringHint3"
}
],
"name": "String"
}
]
Get the individual components from Date object and place them on template file as per desired behavior
let day = debit[0].date.getDate();
let month = debit[0].date.getMonth();
let year = debit[0].date.getFullyear();
I am trying to get data using elastic search in a python program. Currently I am getting the following data from an elastic search request. I wish to sort the data on rank:type. For example i want to sort data by raw_freq or maybe by score.
What should the query look like?
I believe it will be something using nested query. Help would be very much appreciated.
{
"data": [
{
"customer_id": 108,
"id": "Qrkz-2QBigkG_fmtME8z",
"rank": [
{
"type": "raw_freq",
"value": 2
},
{
"type": "score",
"value": 3
},
{
"type": "pmiii",
"value": 1.584962
}
],
"status": "pending",
"value": "testingFreq2"
},
],
}
Here is a simple example of how you can sort your data:
"query": {
"term": {"status": "pending"}
},
"sort": [
{"rank.type.keyword": {"order" : "desc"}}
]
My data:
{
"rootElement": {
"names": {
"name": [
"Haseb",
"Anil",
"Ajinkya",
{
"city": "mumbai",
"state": "maharashtra",
"job": {
"second": "bosch",
"first": "infosys"
}
}
]
},
"places": {
"place": {
"origin": "INDIA",
"current": "GERMANY"
}
}
}
}
I created a hash index on job field with the API:
http://localhost:8529/_db/_api/index?collection=Metadata
{
"type": "hash",
"fields": [
"rootElement.names.name[*].jobs"
]
}
And I make the search query with the API:
http://localhost:8529/_db/_api/simple/by-example
{
"collection": "Metadata",
"example": {
"rootElement.names.name[*].jobs ": "bosch"
}
}
Ideally, only the document containing job : bosch should be returned as a result. But for me it gives all the documents in the array name[*]. Where I am doing mistake?
Array asterisk operators are not supported by simple queries.
You need to use AQL for this:
FOR elem IN Metadata FILTER elem.rootElement.names.name[*].jobs = "bosch" RETURN elem
You can also execute AQL via the REST interface - However you should rather try to let a driver do the heavy lifting for you.
I've a collection named bikes like this:
{
"fname": "foo",
"indian":"hero-corps"
"brands": [
{
"region": "asia",
"type": "terrain"
}
]
}
And getting a json through post like this (let's name it jsonBody):
{
"indian": "hero-corps",
"someKeyA": "someValueA",
}
I'm using the following mongo update query :
db.collection(bikes).update({"indian":"hero-corps"},{$set:jsonBody}, {upsert:true});
The problem is that it's upserting inside the main object, I want to upsert only inside the nested object brands with the jsonBody. How do I achieve that ?
Actual result:
{
"fname": "foo",
"indian": "hero-corps",
"brands": [
{
"region": "asia",
"type": "terrain"
}
],
"indian": "hero-corps",
"someKeyA": "someValueA",
}
Expected result:
{
"fname": "foo",
"indian": "hero-corps",
"brands": [
{
"region": "asia",
"type": "terrain",
"someKeyA": "someValueA",
}
]
}
i'm not sure if that is what you want to do but i'm sure that it will give the format you want.
db.collection(bikes).update({"indian":"hero-corps"},{$push:{"brands":jsonBody}}, {upsert:true});