How to add different values to unique keys in Realm? - java

I'm unable to wrap my head around a problem I'm facing.
I have various json keys, let's say:
Name, grades, skills.
Now for each name, name, there can be multiple grades and skills, so the json could be like this:
[
{
"name": "name1"
"age": "age1"
"skills": "skills1"
},
{
"name": "name1"
"age": "age2"
"skills": "skills2"
},
{
"name": "name1"
"age": "age3"
"skills": "skills3"
}
]
Notice how name isn't changing, but skills and age are. In this case, how do I map all the different ages and skills to that one single name? I don't know the number of unique names in advance.
Let me know if I've missed out any details. Thank you.

Use JSON array like this:
[
{
"name" : "name1",
"age" : ["age1","age2","age3"],
"skills" : ["skills","skill2"]
},
{
"name" : "name2",
"age" : ["age1","age2","age3"],
"skills" : ["skills","skill2"]
}
]

Related

jsonschema2pojo: type creation for list of objects

I am using jsonschema2pojo (via gradle) to generate pojo from an external json schema that has a property (inside another property of type "object") with the following definition (field names and values changed but all types are identical):
"alternativeText": {
"type": "array",
"items": [{
"type": "object",
"required": [
"code",
"language",
"value"
],
"properties": {
"code": {
"type": "integer",
"enum": [1, 2, 3, 4]
},
"language": {
"type": "string",
"enum": ["de-DE", "en-US"]
},
"value": {
"type": "string"
}
}
}],
which generates the following java code
#JsonProperty("alternativeText")
#Valid
#NotNull
private List<Object> alternativeText = new ArrayList<Object>();
#JsonIgnore
#Valid
private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
This interface is not ideal, especially when the consumer is going to validate against the enum values for "code" and "language".
We have similar issues when an enum is in an array, then the API generates List<Object> instead of e.g. List<SomeEnumType>.
Is there anything that can be configured to improve the semantics of the generated code? For other properties with "enum" (and object for that matter!) it works correctly, it seems like the array of enum or array of objects gets overly simplified.
It looks like you have accidentally used the (old) tuple syntax for items. Try replacing this:
"items": [{
with this
"items": {
Your items schema should not be inside an array.

Vespa.ai: How to make an array of floats handle null values?

I am trying to make a simple Vespa application, where one of my data fields are an Array. However the array contains some null values. For instance an array like: [2.0,1.4,null,5.6,...].
What can I use instead of float to represent elements in the array?
Seems like you want to use a sparse tensor field instead since some addresses does not have a value. x{} denotes a sparse tensor, x[128] is an example of a dense tensor. See https://docs.vespa.ai/documentation/tensor-user-guide.html for an intro to vespa tensor fields.
field stuff type tensor<float>(x{}) {
indexing: summary |attribute
}
[
{ "put": "id:example:example::0", "fields": {
"stuff" : {
"cells": [
{ "address" : { "x" : "0" }, "value": 2.0 },
{ "address" : { "x" : "1" }, "value": 1.4 },
{ "address" : { "x" : "3" }, "value": 5.6 },
]
}
}
}
]

Update the nested JSONObject using java

I am new to java programming, I am not getting the right example for my below nested json object scenario, Here is my Json object which I have, how can I update the "stackoverflow" values 1 with 100 and 2 with 200 etc.
also stackoveflow is incremental json object it can be {} or {...}, in below i given size of 2 elements . how can I update/replace this jsonobject faster and efficient way without effecting other objects. Thanks Much
{
"name": "sample",
"stackoverflow":
{
"one": {
"name": 1,
"type": "number",
"value":"onevalue"
},
"two": {
"name": 2,
"type": "number",
"value":"twovalue"
},
},
}

Filter the returned elements in an array after a mongodb query

If I understood correctly this is called a projection in mongodb. I have seen that there are two types of useful projections operators: elemMatch and $.
I have a set of documents that each have a list of attributes(Object). I want to perform complex queries to match these attributes namely matching several of its fields(using regex, etc).
Right now when a document matches a query the Java driver is returning the whole document. I want to filter it to just return the attributes which matched the original query.
I have tried combining elemMatch but all I was able to accomplish was returning either all attributes or just one(normally the first match).
Mongo document structure as below :
{
"name": "MediaPlayer",
"attributes": [
{
"tag": "media",
"values": [
"mp3",
"mp4"
]
},
{
"tag": "teste",
"values": [
"teste"
]
}
]
}
and my query as :
{attributes : {$elemMatch: { 'tag' : 'media'},$elemMatch: { 'tag' : 'stream'}}}
and it return following results :
{
"name": "MediaStream",
"attributes": [
{
"tag": "media",
"values": [
"Edit"
]
},
{
"tag": "stream",
"values": [
"Edit"
]
},
{
"tag": "video",
"values": [
"Edit"
]
}
]
}
The example and returned are different documents. Although I matched two attributes I got the whole list returned.
I wanted the attributes array to contain only the matched elements.
How would I be able to do this?
EDIT:
From what I have seen the mongodb find operation does not support internal array filtering. There are some examples using the mongodb aggregation framework. Other than that doing the filtering manually seems like the only possible way.
Let's suppose you want to match tag:media and tag:stream, this case you entered your matching criteria in mongo $in aggregation query as below
db.collectionName.aggregate({
"$unwind": "$attributes" // first unwind attributes array
}, {
"$match": {
"attributes.tag": {
"$in": ["media", "stream"] // put all matching tags in array using $in
}
}
}, {
"$group": {
"_id": "$_id",
"name": {
"$first": "$name"
},
"attributes": {
"$push": "$attributes" // push all attributes to get only matching tags
}
}
}).pretty()
Found a possible solution : Solution A
EDIT: The aggregation found does not prove to offer a general solution to the problem since i am not controlling the queries. In this sense, if i want to unwind the attributes and then match it against the query it will only work on queries matching a single attribute.

Android - Java - Get nth element in JSONObject

My JSON file contains several attributes. One of them is a list of objects. I need to access this list via a numerical key, ie the 1st, the 2nd etc element.
When getting the nth element I want to access its attributes by a alphabetical key.
Example:
MyObj.get("itemlist").get(0).get("attribute")
If I do this I'm forced to convert the whole thing to an JSONArray from which (afaik) I can't access my attributes via a key but just by position.
Here's my JSON string:
{
"id": 1,
"items": [
{
"id": 1,
"type": "video",
"name": "test.mp4"
},
{
"id": 2,
"type": "image",
"name": "pic.jpg"
}
],
"name": "test"
}
Any ideas?
Ok, don't quite understand why but when I do the following it works:
JSONArray MyList = new JSONObject(filePath).getJSONArray("items");
System.out.println((((JSONObject) MyList.get(1)).get("type")));
So I just omitted the MyObj and targeted the list directly.

Categories

Resources