How to update one cell in a json file - java

I have (java) class of user with some field (username , type,password,country) and json file with the data.
for example -
{
"users": [
{
"username": "john",
"type": "admin",
"password": "potato",
"country": "united states",
},
{
"username": "lisa",
"type": "normal",
"password": "chips123",
"country": "spain",
}
]
}
Is there any way I can give json a particular user object, rather than an array of all users, to update only that specific user data?
and to add more user rather than send new array with all the old and the new users?
The purpose of the program is to eventually build a server, and I have to save all the data in the json file and update them.
Thanks in advance :)

Related

Create options for custom checkbox field using Jira rest api

I am trying to create options for my custom checkbox field.
I need to do it dynamically for each issue. (Some of them can have different options). Is there a way to do it? If yes, what I do wrong?
{"update" : {},
"fields": {"summary": "Title",
"customfield_11357": {
"type": "array",
"items": "option",
"custom": "com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes",
"customId": 11357,
"set" : [
{"name":"Name1"},
{"name":"Name2"}
]
},
"issuetype": {"id": "10255"},
"parent": {"key": "DC-34511"},
"project": {"id": "11738"},
"reporter": {"id": "5dd3e833b63c210fr4b67g6e"}}}

Sort the JSON Body of HTTP Request alphabetically [duplicate]

This question already has answers here:
How to create json, sorted on keys, using gson?
(3 answers)
Closed 2 years ago.
I am doing an API call with certain parameters. The body of the Request is something like this:
{
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"failureUrl": "https://baseurl/ordercomplete/failure",
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"syncOnly": true,
"deliveryMode": "API"
}
I want to sort the parameters of the request alphabetically. The sorting should be done at outer level and inner level as well. For example, address should come before billing after the sort. Within the internal JSON also I want it to be sorted. For example in the billing struct email should come before lastname.
So the answer that I am looking for is:
{
"address": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com",
"telephone": "+919999999999"
},
"billing": {
"firstname": "John",
"lastname": "Master",
"email": "abc.com"
},
"deliveryMode": "API",
"failureUrl": "https://baseurl/ordercomplete/failure",
"payments": [
{
"code": "abcd",
"amount": 500
}
],
"products": [
{
"sku": "sampleSKU",
"price": 500,
"qty": 1,
"currency": 356,
"giftMessage": "",
"theme": ""
}
],
"refno": "abcd123",
"successUrl": "https://baseurl/ordercomplete/success",
"syncOnly": true
}
I think I can do this by creating multiple POJO class having all the field and then implement a comparator which will sort it alphabetically. But this way of doing will make it very difficult even if a single field in the parameter of the request body change.
So I was thinking can we do it some better way where we do not have to worry about the field structure.
You could use Jackson ObjectMapper and configure ObjectMapper as
om.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
Hope it was useful.

How can I store and update the nested json object into couchbase using java sdk

I am using couchbase Community Edition 5.0.1 and java-client 2.7.4. I want to store the following nested json object into couchbase. If I want to update the same object without affecting the other fields.
Eg:
If I want to add one more player object under players object
array
If I want to add One more group say 'Z Group' under group object array
How can I Achieve this without affecting other fields.
{
"doctype": "config:sample",
"group": [{
"name": "X Group",
"id": 1,
"players": [{
"name": "Roger Federer",
"number": 3286,
"keyword": "tennies"
},
{
"name": "P. V. Sindhu",
"number": 4723,
"keyword": "badminton"
}
]
},
{
"name": "Y Group",
"id": "2",
"players": [{
"name": "Jimmy Connors",
"number": 5623,
"keyword": "tennies"
},
{
"name": "Sachin",
"number": 8756,
"keyword": "Cricket"
}
]
}
]
}
N1QL has a huge variety of functions to operate on arrays:
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/arrayfun.html
In your case, you could simply use ARRAY_INSERT or ARRAY_PREPEND
Check out update/update-for syntax (last example) https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html
UPDATE default AS d
SET d.group = ARRAY_APPEND(d.group, {......})
WHERE .....;
UPDATE default AS d
SET g.players = ARRAY_APPEND(g.players, {......}) FOR g IN d.group WHEN g.id = 2 END
WHERE .....;
If you know which document IDs you want to update you can use the key-value subdocument API, which will generally be faster than going via N1QL for a single document update.
This will add a new player to the end of X Group's "players" array:
bucket.mutateIn(docId)
.arrayAppend("group[0].players",
JsonObject.create()
.put("name", "John Smith"))
// ... other player JSON
.execute();
And this will add a new Group Z to the "group" array:
bucket.mutateIn(docId)
.arrayAppend("group",
JsonObject.create()
.put("name", "Z Group"))
// ... other group JSON
.execute();

Show JSON data in grid in vaadin

Am a new bee to vaadin. I have to show the data from a JSON file (which is fetching from MySQL db) in Grid/Table(vaadin). I am able show the data in table if JSON in the below format.
[
{
"id": "ex-wardrobe",
"productId": "ex-wardrobe",
"name": "exWardrobe",
"desc": "Some description",
"dimension": "WxDxH 148\" X 24\" X 112\" ",
"category": "Bedroom",
"subcategory": "Wardrobe",
"categoryId": "bedroom",
"subcategoryId": "wardrobe",
"tags": "all, Space Design Bedroom, Space Details Wardrobe",
"designer": "hb",
"curr": "INR",
"popularity": "1",
"relevance": "1",
"shortlisted": "1",
"likes": "1",
"createDt": "",
"pageId": "ex-wardrobe",
"styleName": "Fresh",
"styleId": "cfresh",
"priceRange": "Premium",
"priceId": "premium",
"defaultPrice": "123",
"defaultMaterial": "MDF ",
"defaultFinish": "LAMINATE"
}
]
But, if i get JSON(data is related to same product) in the below format am unable to add data in table.
[
{
"id": "ex-wardrobe",
"productId": "ex-wardrobe",
"name": "exWardrobe",
"desc": "Some description",
"dimension": "WxDxH 148\" X 24\" X 112\" ",
"category": "Bedroom",
"subcategory": "Wardrobe",
"categoryId": "bedroom",
"subcategoryId": "wardrobe",
"tags": "all, Space Design Bedroom, Space Details Wardrobe",
"designer": "hb",
"curr": "INR",
"popularity": "1",
"relevance": "1",
"shortlisted": "1",
"likes": "1",
"createDt": "",
"pageId": "ex-wardrobe",
"styleName": "Fresh",
"styleId": "cfresh",
"priceRange": "Premium",
"priceId": "premium",
"defaultPrice": "123",
"defaultMaterial": "MDF ",
"defaultFinish": "LAMINATE",
"mf": [
{
"basePrice": "123",
"material": "MDF ",
"finish": "LAMINATE"
}
],
"images": [
"066___ex_WARDROBE_Dim.jpg",
"067___ex_WARDROBE_close_door.jpg",
"068___ex_DOVE_dim.jpg"
],
"components": [],
"accessories": []
}
]
This is the code which am using to show JSON data in table,
Table grid = new Table();
root.addComponent(grid);
grid.setStyleName("iso3166");
grid.setPageLength(6);
grid.setSizeFull();
grid.setSelectable(true);
grid.setMultiSelect(false);
grid.setImmediate(true);
grid.setColumnReorderingAllowed(true);
grid.setColumnCollapsingAllowed(true);
try {
JSONArray products = productsDataProvider.getCatalogs();
JsonContainer dataSource =
JsonContainer.Factory.newInstance(products.toString());
grid.setContainerDataSource(dataSource);
grid.setColumnReorderingAllowed(true);
grid.setWidth("98%");
grid.addStyleName(ChameleonTheme.TABLE_STRIPED);
} catch (IllegalArgumentException ignored) {
}
grid.setWidth("100%");
grid.setHeight("100%");
root.addComponent(grid);
Am stuck on this and i have sleepless night on this. Million tons of thanks in advance. I hope you GURU's can help me in this :)
Sorry not vaadin expert. See it first time and like it. I guess your problem are the arrays inside your object. I mean this:
"mf": [
{
"basePrice": "173881",
"material": "MDF ",
"finish": "LAMINATE"
}
],
"images": [
"066___ex_WARDROBE_Dim.jpg",
"067___ex_WARDROBE_close_door.jpg",
"068___ex_DOVE_dim.jpg"
],
"components": [],
"accessories": []
No idea how the component should display this. Have you tried it without mf, images, components and accessories?
You are using a very simple JSONContainer. As can be seen in the source code, this implementation does not support nested / compound elements and arrays.
First you have to ask yourself how these complex objects need to be displayed (UX), especially the "mf" field.
UPDATE
Simple compound objects (like "simpleCompound": {"name": "foo", number: 123}) can be shown in a table column (not supported by the JSONContainer you use, but similar functionality is available by the BeanItemContainer, so look there for how to implement this functionality).
The array fields are more problematic from a UX standpoint. Mostly this information is only shown on demand or in separate panels. The Vaadin Grid component offers the possibility to show a details view, see the wiki. Maybe that will fit your requirements.
Currently nested json data is not supported unless you create your own advance template for that. There is currently an online json container application demo that is used to test if the data is really json or json array but not nested. Then it displays that data in grid table. So you can use this to verify your data.
You can also get the application template with source code on github

Navigating through JSONObject Android

I am trying to implement a method which is able to navigate through a JSONObject until it finds a given parameter. My JSON is structured like a foldersystem. There are folders and every folder can have files.A folder can have another folder and so on.JSON is looking like this:
{
"Elements": [
{
"file": {
"files": [
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild1"
},
{
"id": "0efd76e7-730e-428a-96a4-95e04844070a",
"modified": 1457608018166,
"name": "Audio"
},
{
"files": [
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild2"
},
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild3"
},
{
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"modified": 1457608018166,
"name": "Bild4"
}
],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "FolderInRoot"
}
],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "RootFolder"
}
},
{
"file": {
"files": [],
"id": "562dd7a2-9268-46b2-8963-b7a3b43e906e",
"name": "AnotherRootFolder"
}
}
]
}
So for example I want to edit "Bild2" which has the path "/RootFolder/FolderInRoot/Bild2". Has someone a method which navigates to that given position. The path should be the parameter.
You need to loop through the your JSON response until you get to the correct depth, and then you can use getString("name") to get the content at that key (i.e. Bild2). Once you get the content from the object/key you can add it to a List or Map to iterate over or do anything else you want with the data. See this StackOverflow question for more info: Java loop over Json array?
You also might want to consider using the Gson library to make your parsing a lot easier. If you are able to add Gson to the project (and I suggest that you do), there are a ton of great tutorials for parsing data and deserializing JSON into plain Java objects (which will save you the headache of nested loops). Using GSON in Android to parse a complex JSON object and http://blog.nkdroidsolutions.com/how-to-parsing-json-array-using-gson-in-android-tutorial/ are two good resources to begin with.
(P.S. you might want to consider simplifying your JSON structure if possible because it will be a lot easier to work with).

Categories

Resources