I have JSON string with dynamic elements, till now I parse it into Map:
Map map = new Gson().fromJson(jsonString,
new TypeToken<HashMap<String, String>>() {}.getType());
Now I need to solve thsi situation - one of these dynamic variables could be another JSON string.
Do you have some advice ho to solve it? Thanks in advance.
EDIT: JSON string example added (formatted):
{
"key1": "val1",
"key2": "val2",
"key3": {
"subkey1": [
"subvalue1",
"subvalue1"
],
"subkey2": [
"subvalue2"
]
},
"key4": "val3"
}
What you call another JSON string is just a json object. Change the Map value type to Object from String: TypeToken>
String jsonString = "{\"key1\":\"val1\",\"key2\":\"val2\",\"key3\": {\"subkey1\":\"subvalue1\",\"subkey2\":\"subvalue2\"},\"key4\":\"val3\"}";
Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>() {
}.getType());
The above example works with GSON 2.2.2. And sysout(map) produces
{key1=val1, key2=val2, key3={subkey1=subvalue1, subkey2=subvalue2}, key4=val3}
As a small improvement I'd suggest that you explicitly specify map type parameters, and use Map instead of HashMap for the TypeToken.
Related
I am trying to access the data inside the X-Amz-Content-Sha256 parameter, but the X-Amz-Content-Sha256 key is different for each request so I cannot hardcode the key value.
Is there a way to access an object without knowing its key, when using mapper.readValue()?
"components": {
"parameters": {
"X-Amz-Content-Sha256": {
"name": "X-Amz-Content-Sha256",
"in": "header",
"schema": {
"type": "string"
},
}
}
}
You need to deserialize it to Map. Then you can check Map key to get desired values.
Sample code to deserialize JSON string to Map:
ObjectReader reader = new ObjectMapper().readerFor(Map.class);
Map<String, Object> jsonMap = reader.readValue(jsonString);
I have spent a few days googling this various ways and don't see any that give examples of using HashMap - instead they all refer to Jackson or GSON. I am not able to use these as they cause an issue in Jenkins that will not be addressed (basically everything is super locked down and the work place will not "open" up alternatives)
I have a JSON body that I am attempting to send to a create record API.
For simple JSON body the process is pretty straightforward:
Desired JSON:
{
"owner": {
"firstName": "Steve",
"lastName": "Guy",
"Hair": "brown",
"Eyes": "yes"
"etc": "etc"
},
"somethingElse": "sure"
}
would look like
Map<String,Object> jsonRequest = new HashMap<>();
Map<String,String> ownerMap = new HashMap<>();
HashMap<Object, String> OwnerMap = new HashMap<Object, String>;
OwnerMap.put("firstName","Steve");
OwnerMap.put("lastName","Guy");
OwnerMap.put("Hair","brown");
OwnerMap.put("Eyes","yes");
OwnerMap.put("etc","etc");
jsonRequest.put("owner", OwnerMap);
jsonRequest.put("somethingElse", "sure");
Easy enough
If the JSON gets slightly more complex, I can't seem to figure it out.. and again I cannot use any other dependency for this.
so if I have a JSON Body that I need to send :
{
"customer": {
"address": [
{
"address": "Blah"
}
]
},
"anotherThing": "thing"
}
the same pattern does not work.
Map<String,Object> jsonRequest = new HashMap<>();
Map<String,String> ownerMap = new HashMap<>();
HashMap<Object, String> addressMap = new HashMap<Object, String>;
addressmap.put("address","Blah");
jsonRequest.put("address", addressMap);
jsonRequest.put("owner", OwnerMap);
jsonRequest.put("anotherThing", "thing");
returns as:
{
"owner": {
},
"anotherThing": "thing",
"address": {
"address": "Blah"
}
}
You seem to assume that the inner (for want of a better word) Maps need to be Map<*, String>, and that Map and String are the only things which extend Object.
Something like the following should work fine:
Map<String, Object> json = new HashMap<>();
Map<String, Object> customer = new HashMap<>();
// Could make this a Map<String, Object>[] (array) depending
// on json library used... You don't specify.
List<Map<String, Object>> address = new ArrayList<>();
Map<String, Object> innerAddress = new HashMap<>();
innerAddress.put("address", "Blah");
address.add(innerAddress);
customer.put("address", address);
json.put("customer", customer);
json.put("anotherThing", "thing");
I need to read and parse a json array from java properties file. I'm able to read the properties file and get the json array String. But I'm unable to parse the json array and get the values. I'm using gson library. Here is the data from properties file
jsonarray = [ { key1 : value1, key2 : value2 } ]
Code:
Properties properties = new Properties();
properties.load(new FileInputStream(filePath));
String jsonArray = properties.getProperty("jsonarray")
JsonElement element = new JsonPrimitive(jsonArray);
JsonArray array = element.getAsJsonArray();
I'm getting IllegalStateException
java.lang.IllegalStateException: Not a JSON Array: "[ { key1 : value1, key2 : value2 } ]"
The problem is it is taking extra double quotes at the beginning and at the end.
I have tried adding quotes to keys and values, but with no success. I need to use gson library itself, so please don't suggest other libraries. I cannot create a POJO class for it and use new Gson().fromJson(jsonArray), so kindly please don't suggest that also.
I've searched and tried a lot without success. The method I referred is given here
Thanks in advance
Edit 1: I've tried the following in properties file
jsonarray = [ { "key1" : "value1", "key2" : "value2" } ]
and I got same exception like
java.lang.IllegalStateException: Not a JSON Array: "[ { \"key1\" : \"value1\", \"key2\" : \"value2\" } ]"
I tried to create a JsonArray object in code and print it
JsonArray array = new JsonArray();
JsonObject object = new JsonObject();
object.addProperty("key1", "value1");
object.addProperty("key2", "value2");
array.add(object);
System.out.println(array.toString());
The output was as below
[{"key1":"value1","key2":"value2"}]
The Javadoc of JsonPrimitive you've used here
JsonElement element = new JsonPrimitive(jsonArray);
states
Create a primitive containing a String value.
When you then do
JsonArray array = element.getAsJsonArray();
it'll obviously fail because element is a JSON String, not a JSON array. Just parse the JSON directly
JsonArray array = new Gson().fromJson(jsonArray, JsonArray.class)
This is all after you've changed your properties file content to be actual JSON
jsonarray = [ { "key1" : "value1", "key2" : "value2" } ]
I'm getting this error when attempting to parse some JSON previously generated with Jackson. I generate the JSON like so
String ret = "";
ret = mapper.writeValueAsString(message.getPayload());
message.setPayload(ret);
Where message.getPayload() is a HashMap, in this instance containing two strings and a List of various objects. This creates the following malformed JSON
{
"user" : "john d example",
"items" : [ {
"val" : 99.5,
"id" : "phone",
"qty" : 1
}, {
"val" : 15.5,
"id" : "wine",
"qty" : 4
} ],
"address" : "123 example street"
}
Which throws an exception when examined thusly
Map<String, Object> ret = new HashMap<String, Object>();
String s = (String)message.getPayload();
ret = mapper.readValue(s, new TypeReference<Map<String, String>>(){});
How should I properly write this Map to JSON?
TypeReference<Map<String, String>> should be TypeReference<Map<String, Object>>. Jackson is attempting to parse the values as Strings rather than Lists because that is what it expects based on the TypeReference you passed in.
I am new To JSon and i want to search the following json string and get the required output.
String:
{"status":"Success","code":"200","message":"Retrieved Successfully","reason":null,"
"projects":
[
{
"projectName": "example",
"users":
[
{
"userName": "xyz",
"executions":
[
{
"status": "check",
"runs":
[
{
"Id": "------",
"Key": "---"
}
],
"RCount": 1
}
],
"RCount": 1
}
],
"RCount": 1
},
Like that i have many projects and now , if i give projectname and username as input i wantt to get its status as output.
Is it possible?If yes how?
You may use JSONObject for this.
JSONObject json = new JSONObject(string);
JSONArray[] projectsArray = json.getJSONArray("projects");
for(int i = 0; i < projectsArray.length; ++i)
{
String projectName = projectsArray[i].getString("projectName");
...
}
Use the same method to get the users.
You can use gson library. Using gson convert your json string to Map and then you can iterate through map to get required item
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(jsonString, type);
You can use the Google gson to map your json data structure to a Java POJOs.
Example :
You can have Projects class containing list/array of Users.
Users class containing list/array of Executions and so on.
Gson library can easily map the json to these classes as objects and you can access your data in a more elegant manner.
Here are a few references :
http://howtodoinjava.com/2014/06/17/google-gson-tutorial-convert-java-object-to-from-json/
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/