Convert JSON string to DynamoDB LastEvaluatedKey - java

I have tried using Gson
Gson gson = new Gson();
Map<String, AttributeValue> attributes = gson.fromJson(paginationTokenString, new TypeToken<Map<String, AttributeValue>>() { }.getType());
For the JSON string
{
"createdBy": {
"S": "1234567"
},
"caseCreationDateTime": {
"S": "2022-05-02T10:38:16.574590Z"
},
"caseId": {
"S": "534kjnsjf"
}
}
I'm getting the output something like this {createdBy={}, caseCreationDateTime={}, caseId={}}
Is there any other way to convert JSON String to Map<String, AttributeValue>?

Related

Gson append new object array to existing JSON file

i need some help appending new arrays into a existing file. I have a JSON file like this:
[
{
"name": "any",
"address": {
"street": "xxxx",
"number": 1
},
"email": "teste#gmail.com"
}
]
I want to insert new array, so my file will be like this:
[
{
"name": "any",
"address": {
"street": "xxxx",
"number": 1
},
"email": "test#gmail.com"
},
{
"name": "any2",
"address": {
"street": "yyyyy",
"number": 2
},
"email": "test2#gmail.com"
}
]
Here's my code:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
ArrayList<Person> ps = new ArrayList<Person>();
// .... reading entries...
ps.add(new Person(name, address, email));
String JsonPerson = gson.toJson(ps);
File f = new File("jsonfile");
if (f.exists() && !f.isDirectory()) {
JsonReader jsonfile = new JsonReader(new FileReader("jsonfile"));
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonfile);
//here goes the new entry?
try (FileWriter file = new FileWriter("pessoas.json")) {
file.write(JsonPessoa);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
So, what's the best way to do this?
Thanks in advance.
Gson really shines when combined with Pojos, so my suggestion would be use of mapped pojos. Consider below two classes.
public class Contact {
#SerializedName("address")
private Address mAddress;
#SerializedName("email")
private String mEmail;
#SerializedName("name")
private String mName;
// getters and setters...
}
public class Address {
#SerializedName("number")
private Long mNumber;
#SerializedName("street")
private String mStreet;
// getters and setters...
}
Read JSON and add new contact and convert it back to JSON, It also works for other way around seamlessly. Similarly you can use this approach for solve many use cases. Pass json array string by reading from file or using similar way, after
Gson gson = new Gson();
List<Contact> contacts = gson.fromJson("JSON STRING", new TypeToken<List<Contact>>() {}.getType());
Contact newContact = new Contact();
// set properties
contacts.add(newContact);
String json = gson.toJson(contacts);
There are tools like this one to create pojos from JSON.

Java - Merge JSON files

I am building an updater util that update my application.
I need to save my configuration files from the previous version.
Some of the configurations files are JSON files.
Is there a way to update the JSON files in a way that i'm keeping the old values
but if there new objects or keys they will be saved also?
For example:
Version 1 json file:
{
"name": "demo",
"description": "Parse some JSON data",
"location": "New York"
}
Version 2 json file:
{
"name": "demo",
"description": "Parse some JSON data",
"location": "London",
"day": "Monday"
}
Expected json file after the merge:
{
"name": "demo",
"description": "Parse some JSON data",
"location": "New York",
"day": "Monday"
}
Is there a way to do so without any external library?
As #cricket_007 advised, This solution worked for me:
public void mergeJsonsFiles(File newJson, File oldJson) throws Exception {
HashMap<String, Object> newMap = convertJsonToMap(newJson);
HashMap<String, Object> oldMap = convertJsonToMap(oldJson);
for (Entry<String, Object> entry : oldMap.entrySet()) {
if (newMap.get(entry.getKey()) == null) {
newMap.put(entry.getKey(), entry.getValue());
}
}
ObjectMapper mapper = new ObjectMapper();
String jsonFromMap = mapper.writeValueAsString(newMap);
PrintWriter writer = new PrintWriter(newJson);
writer.write(jsonFromMap);
writer.close();
}
private HashMap<String, Object> convertJsonToMap(File json) {
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = new HashMap<String, Object>();
try {
map = mapper.readValue(json, new TypeReference<HashMap<String, Object>>(){});
} catch (IOException e) {
map.clear();
}
return map;
}

new JSONObject(Map map) not giving desired result

I need to send a POST request with json payload, and it's a requirement that the whole project is lightweight, so it's just a simple java project, and I'm using java.net.HttpURLConnection and org.json.JSONObject.
This method compiles my payload:
public static String compileSRF() throws JSONException{
Map<String, Boolean> flags = new HashMap<String, Boolean>();
flags.put("overrideStore", true);
flags.put("matchmaking", true);
JSONObject orchestrationFlags = new JSONObject(flags);
JSONObject requesterSystem = new JSONObject();
JSONObject requestedService = new JSONObject();
requesterSystem.put("systemGroup", "testGroup");
requesterSystem.put("systemName", "testSystem");
requesterSystem.put("address", "localhost");
requestedService.put("serviceGroup", "Temperature");
requestedService.put("serviceDefinition", "IndoorTemperature");
List<String> interfaces = new ArrayList<String>();
interfaces.add("json");
requestedService.put("interfaces", interfaces);
JSONObject payload = new JSONObject();
payload.put("requesterSystem", requesterSystem);
payload.put("requestedService", requestedService);
payload.put("orchestrationFlags", orchestrationFlags);
return payload.toString();
}
The produced payload looks like this:
{
"orchestrationFlags": {
"overrideStore": true,
"matchmaking": true
},
"requesterSystem": {
"address": "localhost",
"systemName": "testSystem",
"systemGroup": "testGroup"
},
"requestedService": {
"interfaces": ["json"],
"serviceGroup": "Temperature",
"serviceDefinition": "IndoorTemperature"
}
}
But when this payload gets to my web server, and the code tries to parse the "orchestrationFlags" hashmap, it does not succeed, and uses default values instead. When I did the testing for the code on the web server, this is the payload structure I've always used in Postman and it worked:
{
"orchestrationFlags": {
"entry": [
{
"key": "overrideStore",
"value": true
},
{
"key": "matchmaking",
"value": true
}
]
},
//requesterSystem and requestedService is the same
}
How can I achive this with JSONObject? (or with another simple API, but maven import is not an option)
Thank you!
Use List<Map<String,Object>> for the entry attribute and put this value in orchestrationFlags json object.
//Refactored code below:
public static String compileSRF() throws JSONException{
List<Map<String,Object>> entryList = new ArrayList<>();
Map<String, Object> flag1 = new HashMap<>();
flag1.put("key", "overrideStore");
flag1.put("value", true);
entryList.add(flag1);
Map<String, Object> flag2 = new HashMap<>();
flag2.put("key", "matchmaking");
flag2.put("value", true);
entryList.add(flag2);
JSONObject orchestrationFlags = new JSONObject();
JSONObject requesterSystem = new JSONObject();
JSONObject requestedService = new JSONObject();
orchestrationFlags.put("entry", entryList);
requesterSystem.put("systemGroup", "testGroup");
requesterSystem.put("systemName", "testSystem");
requesterSystem.put("address", "localhost");
requestedService.put("serviceGroup", "Temperature");
requestedService.put("serviceDefinition", "IndoorTemperature");
List<String> interfaces = new ArrayList<String>();
interfaces.add("json");
requestedService.put("interfaces", interfaces);
JSONObject payload = new JSONObject();
payload.put("requesterSystem", requesterSystem);
payload.put("requestedService", requestedService);
payload.put("orchestrationFlags", orchestrationFlags);
return payload.toString(4);
}
Output:
{
"orchestrationFlags": {"entry": [
{
"value": true,
"key": "overrideStore"
},
{
"value": true,
"key": "matchmaking"
}
]},
"requesterSystem": {
"address": "localhost",
"systemName": "testSystem",
"systemGroup": "testGroup"
},
"requestedService": {
"interfaces": ["json"],
"serviceGroup": "Temperature",
"serviceDefinition": "IndoorTemperature"
}
}
Hope this helps.

How to parse the json object with multiple keys in android

I want to put the JSON result in textviews but because of multiple array i can get only one key/value of datetime, location and status objects. The json object is:
{
"signature":"testSignature",
"deliverydate":"2015-08-06 15:07:00",
"datetime":{
"0":1438848420,
"1":1438841820,
"2":1438838760,
},
"location":{
"0":"PA",
"1":"PA",
"2":"PA",
},
"status":{
"0":"packed",
"1":"On the go",
"2":"delivered",
},
"pickupdate":2015-08-04 07:55:00
}
and this is my java code:
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("NO", NUMBER_TO_POST));
JSONObject json = jsonParser.makeHttpRequest(URL_TO_POST, "POST", params);
success = json.getString(TAG_SIGNATURE);
if (success != null) {
SIGNATURE = json.getString(TAG_SIGNATURE);
DELIVERY_DATE = json.getString(TAG_DELIVERY_DATE);
JSONObject DT = json.getJSONObject(TAG_DATETIME);
DATETIME = DT.getString("0");
JSONObject LOC = json.getJSONObject(TAG_LOCATION);
LOCATION = LOC.getString("0");
JSONObject STAT = json.getJSONObject(TAG_STATUS);
STATUS = STAT.getString("0");
PICKUP_DATE = json.getString(TAG_PICKUP_DATE);
}else{
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
can anyone help me to solve this? Thanks
You should use GSON library to parse JSONs.
And to be a bit more helpful, here is how your class to hold JSON values might look like:
class MyClassForGsonToHoldParseJSON {
String signature;
String deliverydate;
Map<String, long> datetime;
Map<String, String> location;
Map<String, String> status;
String pickupdate;
}
Then just use something like this to conver variable json with JSON data to an object:
Gson gson = new Gson();
MyClassForGsonToHoldParseJSON f = gson.fromJson(json, MyClassForGsonToHoldParseJSON.class);
Your JSON format is wrong:
{
"signature": "testSignature",
"deliverydate": "2015-08-06 15:07:00",
"datetime": {
"0": 1438848420,
"1": 1438841820,
"2": 1438838760
},
"location": {
"0": "PA",
"1": "PA",
"2": "PA"
},
"status": {
"0": "packed",
"1": "On the go",
"2": "delivered"
},
"pickupdate": " 2015-08-04 07:55:00"
}

How to parse this kind of json String using Gson?

{
"status": "Success",
"message": "Contents retrieved successfully",
"name": {
"1": "God",
"2": "Goat"
},
"sites": {
"1": "google",
"2": "yahoo",
"3": "bing"
},
"places": [
"UK",
"AU",
"US"
],
"images": {
"1": {
"1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
"2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
},
"2": {
"1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
"2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
},
"3": {
"1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
"2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
}
}
}
My Class
import java.util.Map;
public class Data {
String status;
String message;
Map<String, String> name;
Map<String, String> Sites;
#Override
public String toString() {
return "Data [status=" + status + ", message=" + message
+ ", name=" + name + ", Sites=" + Sites
+ "]";
}
}
this class returns null value for the while retrieving sites and names
name and sites are JSONObjects no Arrays. Any Object in a JSON have to deserialised in a class using GSON.
So try this,
public class MyJson {
String status;
String message;
Sites sites;
List<String> places;
}
public class Sites {
String 1;
String 2;
String 3;
}
and so on for every Object. For Arrays you can use List / Map.
To use it make a call like this:
Gson gson = new Gson();
MyJson myJson = gson.fromJson(yourJsonString, MyJson.class);
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject)parser.parse(yourString);
for (Map.Entry<String,JsonElement> entry : object.entrySet()) {
JsonArray array = entry.getValue().getAsJsonArray();
for (JsonElement elementJSON : array) {
[...]
}
}

Categories

Resources