Update the nested JSONObject using java - 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"
},
},
}

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 },
]
}
}
}
]

gson - How to compare JsonObjects ignoring few nodes?

I want to compare two JsonObjects (GSON) in Java in such a way that I can ignore some predefined nodes like timestamp, id, etc.
Here is example JsonObject:
Object 1:
{
"id": "uid1",
"name": "name",
"data": {
"timestamp": "timestamp1",
"properties": {
"id": "propId1",
"propertyName": "propertyValue"
}
}
}
Object 2:
{
"id": "uid2",
"name": "name",
"data": {
"timestamp": "timestamp2",
"properties": {
"id": "propId2",
"propertyName": "propertyValue"
}
}
}
In above JsonObjects the comparison should result as identical. "id" and "timestamp" should be ignored.
Since my JsonObjects are nested and complicated I am trying to find a way I could do it avoiding to iterate all nodes. Using "equals" will right now give identical as false.
If these objects are mapped to some entity in your application, you can write a Comparator which compare the equality of two objects based on your criteria of equality

How to add different values to unique keys in Realm?

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"]
}
]

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