How to Query Sublist in CosmosDB to desired output - json

{
"Name": "Sam",
"Car": [
{
"Brand": "BMW",
"Category": "HunchBack",
"Type": "Gas"
},
{
"Brand": "Tesla",
"Category": "Sedan",
"Type": "Electric"
}
]
}
I want to Cosmos Sqlquery to query the sublist CAR on BRAND and it will only return those document that matches the criteria.
Select * from c JOIN t IN c.Car
where t.BRAND = 'Tesla'
I tried this but it only works partially, as it also return the Sublist BMW
But expected output is
{
"Name": "Sam",
"Car": [
{
"Brand": "Tesla",
"Category": "Sedan",
"Type": "Electric"
}
]
}

Try it?
Select distinct c.Name,ARRAY(SELECT n.Brand,n.Category,n.Type FROM n IN c.Car where n.Brand = 'benci') as Car from c JOIN t in c.Car where t.Brand='benci'

Related

Extract nested JSON which have the same value for a specific key

In the following JSON, I want to extract a list of all the nested JSON objects (nested dictionaries) which have the same value for the account_id key.
data =
[
{
"index": 1,
"timestamp": 1637165214.7020836,
"transactions": []
},
{
"index": 2,
"timestamp": 1489565214.7018296,
"transactions": [
{
"account_id": "12",
"device_id": "42",
}
]
},
{
"index": 3,
"timestamp": 1644585214.7012236,
"transactions": [
{
"account_id": "13",
"device_id": "43",
}
]
},
{
"index": 4,
"timestamp": 1644752214.7012756,
"transactions": [
{
"account_id": "13",
"device_id": "44",
}
]
}
]
For the above example, I expect to get the following:
[
{
"index": 3,
"timestamp": 1644585214.7012236,
"transactions": [
{
"account_id": "13",
"device_id": "43",
}
]
},
{
"index": 4,
"timestamp": 1644752214.7012756,
"transactions": [
{
"account_id": "13",
"device_id": "44",
}
]
}
]
Tried iterating over each dictionary but failed to get the desired outcome:
l = []
for entry in data:
l = l + entry['transactions']
print(transaction['ticket_id' for transaction in l if 'ticket_id' in transaction]) # which only lists all the ticket id values
Surely there must be an 'easy' way, perhaps via the json module?
H
for d in data:
if not len(d['transactions']):
continue
acc_id=d['transactions'][0]['account_id']
if acc_id in group_by_id:
group_by_id[acc_id].append(d)
else:
group_by_id[acc_id]=[d]
print(group_by_id)
this would group every entry according to their account_id. Rest you can process according to the use case

Filtering on nested JSON array: Rest Assured

I am trying to filter my GET response(JSON) based on a value in a nested JSON array. For eg: In the following JSON i want to filter an JSON array and print the names of cakes using Chocolate as batter.
{
"id": "0001",
"type": "donut",
"name": "Choco Blueberry Cake",
"ppu": 0.55,
"batter":[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" }
]
}
I have tried something like:
List<String> chocolateCakeList =jsonPath.getList("findAll{it.batter.type=='chocolate'}.name");
and
List<String> chocolateCakeList =jsonPath.getList("findAll{it.batter.it.type=='chocolate'}.name");
Both return empty Lists.
Problem
First, if you want to use findAll then the object to be axtract must be an array. Your json is object {}, not an array []. Actually, your nested object batter is an array [].
keyword to search is Chocolate, not chocolate. Case-sensitive is applied here.
Solution
-If your whole response is exactly what you posted, then the path to extract is
String name = jsonPath.getString("name")
-If your response has structrure like this.
[
{
"id": "0001",
"type": "donut",
"name": "Choco Blueberry Cake",
"ppu": 0.55,
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
}
]
},
{
"id": "0002",
"type": "donut",
"name": "Choco Blueberry Cake",
"ppu": 0.55,
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate 2"
},
{
"id": "1003",
"type": "Blueberry"
}
]
}
]
Then the extraction is
List<String> list = jsonPath.getList("findAll {it.batter.findAll {it.type == 'Chocolate'}}.name");

2 Nested Dictionaries Json format using Json_norminalize using python

Hi I am trying to retrieve this data, there are 2 nested dictionaries within it :
{
"metadata": {
"stations": [
{
"id": "S108",
"device_id": "S108",
"name": "Kuala Lumpur",
"location": {
"latitude": 3.1390,
"longitude": 101.6869
}
},
{
"id": "S118",
"device_id": "S118",
"name": "Bukit Bintang",
"location": {
"latitude":3.1468,
"longitude": 101.7113
}
}
],
"reading_type": "DBT 1M F",
"reading_unit": "deg C"
},
"items": [
{
"timestamp": "2021-06-20T15:05:00+08:00",
"readings": [
{
"station_id": "S108",
"value": 32.6
},
{
"station_id": "S118",
"value": 30.3
}
]
}
]
I wanted to get the result like this :
Result
I have tried a few ways :
data = airtemp.json()
df = pd.json_normalize(data,record_path=['metadata', 'stations'])
df
data = airtemp.json()
df1 = pd.json_normalize(data,record_path=['items','readings'])
df1
Is there a way that I can use json_norminalize to form one table with station_id, name, latitude, longtitude, timestamp and value without breaking into 2 tables ?
Many thanks!
You can merge the two dataframes you created:
import pandas as pd
data = {
"metadata": {
"stations": [
{
"id": "S108",
"device_id": "S108",
"name": "Kuala Lumpur",
"location": {
"latitude": 3.1390,
"longitude": 101.6869
}
},
{
"id": "S118",
"device_id": "S118",
"name": "Bukit Bintang",
"location": {
"latitude": 3.1468,
"longitude": 101.7113
}
}
],
"reading_type": "DBT 1M F",
"reading_unit": "deg C"
},
"items": [
{
"timestamp": "2021-06-20T15:05:00+08:00",
"readings": [
{
"station_id": "S108",
"value": 32.6
},
{
"station_id": "S118",
"value": 30.3
}
]
}
]
}
stations = pd.json_normalize(data, record_path=['metadata', 'stations'])
readings = pd.json_normalize(data, record_path=['items', 'readings'])
result = stations.merge(readings, left_on='id', right_on='station_id')
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(result)
Outputs:
id device_id name location.latitude location.longitude \
0 S108 S108 Kuala Lumpur 3.1390 101.6869
1 S118 S118 Bukit Bintang 3.1468 101.7113
station_id value
0 S108 32.6
1 S118 30.3
There is only one timestamp in the data you provided so you will have to fetch that separately.

How can I return an array of object based on matching ids?

I am trying to match the ids of two json files and return the matching objects. These are the 2 json files:
{
"een": {
"id": "100",
"title": "Entertainment and stuff"
},
"twee": {
"id": "107",
"title": "Sport for everyone"
},
"drie": {
"id": "108",
"title": "Eating is good"
}
}
This is the second one:
[
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false
}
]
As a result of the 2 matching idvalues I should get:
[
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false,
"title": "Entertainment and stuff"
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false,
"title": "Sport for everyone"
}
]
I was wondering if there was a fancy way using lodash or something else and do this in a nice compact way?
One possible solution would be to use merge to merge two objects that have the id as the key. This can be done using keyBy on the array and also on the values of the first object. Intersection is used find ids that are in both arrays.
let list1 = {
"een": {
"id": "100",
"title": "Entertainment and stuff"
},
"twee": {
"id": "107",
"title": "Sport for everyone"
},
"drie": {
"id": "108",
"title": "Eating is good"
}
}
let list2 = [
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false
}
]
let o1 = _.keyBy(_.values(list1), 'id');
let o2 = _.keyBy(list2, 'id');
let matchingIds = _.intersection(_.keys(o1), _.keys(o2));
let result = _.chain(o1)
.pick(matchingIds)
.merge(_.pick(o2,matchingIds))
.values()
.value()
Building on #GruffBunny's answer, you want to take his result and filter out those ids that aren't found in o1 and o2.
let o1 = _.keyBy(_.values(list1), 'id'));
let o2 = _.keyBy(list2, 'id');
let idsToPull = _.difference( _.map(o1, 'id'), _.map(o2, 'id')) //["108"]
let merged = _.values(_.merge(o1, o2 ));
let result = _.filter(merged, function(obj){ return _.indexOf(idsToPull, obj.id) === -1 })

Problem retrieving data from json output

I am a noob when it comes to json. The more I use it, I am starting to like it. I have a output that looks like this
[
{
"product": {
"id": "6",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
}
]
Actually I have listed only one product, But output has many products listed. I want to loop through this json output and get all the product's category, countries where they are available and price using fbjs.
I appreciate any help.
Thanks.
If your JSON is like this,
var products = [
{
"product": {
"id": "6",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
},
{
"product": {
"id": "7",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
}
]
You can iterate through:
for(var index = 0; index < products.length; index++) {
alert(products[index].product.category);
}