I have a JPA entity corresponding to a table in a database in which I have following
#Type(type = "jsonb")
#Column(name = "detail", columnDefinition = "jsonb")
private List<CustomObject> customObjectList;
The detail column in the database is of type JSON, and the entry for the column in the database looks like:
[
{
"field1": "1",
"field2":"2"
},
{
"field1": "3",
"field2": "5"
},
{
"field1": "4",
"field2": "6"
}
]
Where field1 and field2 are attributes of customObject.
But, when I fetch them using a GET API, it gives me a class cast exception
Something like
LinkedHashMap cannot be converted to List<CustomObject>.
Any suggestion how to resolve it?
Related
I have a model with a JSONB field (Postgres).
from sqlalchemy.dialects.postgresql import JSONB
class Foo(Base):
__tablename__ = 'foo'
data = Column(JSONB, nullable=False)
...
where the data field looks like this:
[
{
"name": "a",
"value": 0.0143
},
{
"name": "b",
"value": 0.0039
},
{
"name": "c",
"value": 0.1537
},
...
]
and, given a search_name I want to return the rows where name is in the top x ranked names.
I know I can access the fields with something like:
res = session.query(Foo).filter(Foo.data['name'] == search_name)
but how do I order the JSON and extract the top names from that?
A SQLAlchemy solution would be preferred, but also a plain SQL one I can use as raw is fine.
I need to migrate jsonb (Flyway migration) column from
{
"property1":
{
"type": "A",
"value": "value1"
},
"property2":
{
"type": "B",
"value" "value2"
}
}
into simplified
{
"property1": "value1",
"property2": "value2"
}
So far, I have
update sms_notification n
set param2 = jsonb_object(
array(select jsonb_object_keys(n.params)),
'dummyValue'
)
producing
{
"property1": "dummyValue",
"property2": "dummyValue"
But I can't figure out how to extract the values (value1/value2) from the property1/property2 object.
you need to unnest all keys using jsonb_each() then aggregate the key/values back into a single object:
update sms_notification
set param2 = (select jsonb_object_agg(m.key, m.item -> 'value')
from jsonb_each(param2) as m(key, item));
Thank you for help. I am trying to execute AND/OR operator in GraphQL without Database.
Below is Query need to execute on dataset not database. Please understand, I don't have authority to connect to any database.
{
customVisualsData(_filter: {and: [{expression: {field: "Country", like: "Canada"}},{expression: {field: "Profit", gt: "5000"}}]}) {
Country
DiscountBand
Product
Segment
Profit
}
}
Transform dataset/JSON Object look like this.
[
{
"Country": "Canada",
"DiscountBand": "High",
"Product": "Paseo",
"Segment": "Government",
"COGS": 1477815,
"GrossSales": 2029256,
"ManufacturingPrice": 70,
"UnitsSold": 12230.5,
"Profit": 300289.99999999994
},
{
"Country": "United States of America",
"DiscountBand": "High",
"Product": "VTT",
"Segment": "Small Business",
"COGS": 1461250,
"GrossSales": 1753500,
"ManufacturingPrice": 750,
"UnitsSold": 5845,
"Profit": 74288
}
]
Schema Builder, I used to create GraphQL Query builder.
var schema = buildSchema(`
type customVisualObject {
Country: String
DiscountBand: String
Product: String
Segment: String
COGS: Float
GrossSales: Float
ManufacturingPrice: Int
UnitsSold: Float
Profit: Float
}
type Query {
customVisualsData(_filter: FilterInput): [customVisualObject]
}
input FilterExpressionInput {
field: String!
eq: String
gt: String
gte: String
like: String
}
input FilterInput {
expression: FilterExpressionInput
and: [FilterInput!]
or: [FilterInput]
not: [FilterInput!]
}
`);
Please let me know, if anyone know How to set resolver for this on graphQL?
Does anyone one know JSON-ata Or GraphQL library to execute such complex Query on JSON Object Not databse?
I appreciate your help.
I have a relatively "simple" collection and query requirement.... I need help with the right syntax. :)
Here is the document definition in Java.
public class Entity {
private String _id = null;
private String name = null;
private Map<String, Object> attributes = new Hashmap<String, Object>();
}
Sample data inserted are like
{
"_id": "pk1",
"name": "Item 1",
"attributes": {
"en": { "attr1": "value 1 in English", "attr2": "value 2 in English"},
"es": { "attr1": "value 1 in Spanish", "attr2": "value 2 in Spanish"}
}
}
{
"_id": "pk2",
"name": "Item 2",
"attributes": {
"en": { "attr1": "value 1 in English", "attr2": "value 2 in English"},
"ms": { "attr1": "value 1 in Malay", "attr2": "value 2 in Malay"}
}
}
The language (key) of the map is an example of user provided (thus unpredictable) keys. I want a simple query syntax to be able fetch documents based on the attributes. For example, I want to put in a criteria "'attr1' contains 'value 1'" and query returns both docs, 'Item 1' and 'Item 2'.
Thanks in advance.
I am trying to filter the products based on "cashback" offer using filter, but it is not working.Thanks in advance
My sprinboot method is:
public List<ProductsObj> getAllCashbackItems() {
Query query = new Query();
query.addCriteria(Criteria.where("listOfPromotions").in("cashback"));
Aggregation aggregation = newAggregation(
project().and(filter("listOfPromotions")
.as("item")
.by(valueOf(
"item.promotionName")
.equalToValue(
"cashback")))
.as("listOfPromotions")
);
return mongoTemplate.aggregate(aggregation, "productsObj", ProductsObj.class).getMappedResults();
}
My collection is look alike:
[
{
"_id": "5be29135f590eb09a079951a",
"name": "tennis ball",
"price": "1500",
"category": "sports",
"listOfPromotions": [
{
"promotionName": "cashback",
"percentage": 12
}
]
},
{
"_id": "5be29142f590eb09a079951b",
"name": "volley ball",
"price": "600",
"category": "sports",
"listOfPromotions": [
{
"promotionName": "dham dhamaal",
"percentage": 6
}
]
},
{
"_id": "5be2914ef590eb09a079951c",
"name": "Dinning table",
"price": "15000",
"category": "furnitures",
"listOfPromotions": [
{
"promotionName": "cashback",
"percentage": 6
},
{
"promotionName": "dham dhamaal",
"percentage": 10
}
]
}
]
My ProductsObj POJO class is :-
#Document(collection = "productsObj")
public class ProductsObj {
#Id
public ObjectId _id;
#Indexed
public String name;
public String price;
public String category;
public List<Promotion> listOfPromotions;
//constructors
//getters and setters
}
I am trying filter the object based on above mentioned criteria (i.e., cashback offer) In Promotions entity class contains promotionName and percentage.
I found the solution instead of simple criteria query i tried aggregation.
#RequestMapping(value = "/getcashbackitems", method = RequestMethod.GET)
public List<ProductsObj> getAllCashbackItems() {
Query query = new Query();
query.addCriteria(Criteria.where("listOfPromotions.promotionName").is("cashback"));
return mongoTemplate.find(query, ProductsObj.class);
}
Thanks for your responses :)