I want to parse this object to a list of string. I do not need the key but just want the value as a list of string.
I cannot have a simple model classes because the keys object are more than 1000 in some responses and are random.
So please any idea how to parse it to list in kotlin or java?
{
"data": {
"21": "593754434425",
"22": "4560864343802",
"23": "7557134347529",
"24": "5937544344255",
"25": "45608643438024",
"26": "75571343475293"
}
}
You could first deserialize it as it is, and then convert to a list.
The JSON can be represented this way:
data class Response(val data: Map<String, String>)
You can mark this class #Serializable and use Kotlinx Serialization to deserialize it, or you can use other libraries like Moshi or Jackson (with jackson-module-kotlin).
Once it's deserialized, simply get the values of the map (it's a collection):
val response = Json.decodeFromString<Response>(yourJsonString)
// this is a Collection, not List, but it should be good enough
val stringValues = response.data.values
// if you really need a List<String>
val list = stringValues.toList()
If you want to get the values in the natural order of the keys, you can also use something like:
val values = response.data.toSortedMap(compareBy<String> { it.toInt() }).values
You can use this to parse your data:
val it: Iterator<String> = json.keys()
val arrayList = ArrayList<String>()
while (it.hasNext()) {
val key = it.next()
arrayList.add(json.get(key))
}
A better way is to change the json model, if you access it.
{
"data": [
"593754434425","4560864343802",
"7557134347529","5937544344255",
"45608643438024","75571343475293"
]
}
For this problem, its handy to use the libriary org.json.
See following code snippet:
import org.json.JSONObject;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Defining the input
String input = "{\n" +
" \"data\": {\n" +
" \"21\": \"593754434425\",\n" +
" \"22\": \"4560864343802\",\n" +
" \"23\": \"7557134347529\",\n" +
" \"24\": \"5937544344255\",\n" +
" \"25\": \"45608643438024\",\n" +
" \"26\": \"75571343475293\"\n" +
" }\n" +
"}\n";
// Parsing it to a json object with org.json
JSONObject inputJson = new JSONObject(input);
// If inputJson does not contain the key data, we return
if(!inputJson.has("data")) return;
// Else we read this data object to a new JSONObject
JSONObject dataJson = inputJson.getJSONObject("data");
// Define an array list where all the values will be contained
ArrayList<String> values = new ArrayList<>();
// Get a key set of the dat json object. For each key we get its respective value and add it to our value array list
for (String key : dataJson.keySet()) values.add(dataJson.getString(key));
// Print all values
for (String value : values) System.out.println(value);
}
}
=>
4560864343802
7557134347529
5937544344255
45608643438024
75571343475293
593754434425
Installing org.json is the easiest with a package manager like maven or gradle.
Guys i have comeup with a similar solution for the problem here
this is my model class
data class UnVerifiedTagIds(
#SerializedName("data")
val data: Object
)
and this is how i parse the respone here
val values: ArrayList<String> = ArrayList()
val list_of_tag_ids: ArrayList<String> =response.data as ArrayList<String>
The ist one is the dataclass for the response
and the 2nd one is the ApiCallInterface m using Retrofit...
and the last one is the apicall itself
I am using Kotlin language
do class name with name like this data class Result(val data:Map<String,String>)
and using library GSON for convert string json to this model
val json = "{\n" +
" \"data\": {\n" +
" \"21\": \"593754434425\",\n" +
" \"22\": \"4560864343802\",\n" +
" \"23\": \"7557134347529\",\n" +
" \"24\": \"5937544344255\",\n" +
" \"25\": \"45608643438024\",\n" +
" \"26\": \"75571343475293\"\n" +
" }\n" +
"}"
val dat = Gson().fromJson(json,Result::class.java)
if (dat.data.isNotEmpty()){
val list= dat.data.values.toMutableList()
print(list)
}
that works fine with me
Related
i do have following JSON and i am trying to extract objects inside result
{
"status":true,
"results":{
"type1":{
"id":"type1"
},
"type2":{
"id":"type2"
}
}
}
Desired output is
type1,type2
I am using Gson for serialization and deserialization.
These are the steps you should be doing when using gson
get keys of the json objects which are inside "results" alone
get it as json object which has keys and values
collect the entry set our of the JSON
create an iterator so that later you can extract the keys
Here is code that does the same job:
public static void main(String[] args) throws Exception {
String json = "{\r\n" +
" \"status\":true,\r\n" +
" \"results\":{\r\n" +
" \"type1\":{\r\n" +
" \"id\":\"type1\"\r\n" +
" },\r\n" +
" \"type2\":{\r\n" +
" \"id\":\"type2\"\r\n" +
" }\r\n" +
" }\r\n" +
"}";
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();
//get keys of the json objects which are inside "results" alone
//get it as json object which has keys and values
//collect the entry set our of the JSON
//create an iterator so that later you can extract the keys
Iterator<Entry<String, JsonElement>> iterator = obj.get("results")
.getAsJsonObject()
.entrySet()
.iterator();
while(iterator.hasNext()) {
//here you will get the keys like - type1 and type2
System.out.println(iterator.next().getKey());
}
}
Code Edit: What #fluffy pointed makes complete sense. Made the change
Suppose the GET request returns me some JSON object:
{
"a": 1, "b": 2, "c": 3, "date": "current_date"
}
And I have in my hand a similar object, which I'd like to check it's identical for the the keys "a", "b", "c" and ignore the "date" key.
How can I do that?
I have been using JsonUnit and it really helps
String json1 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"30-07-2020\"\r\n" + "}";
String json2 = "{\r\n" + " \"a\": 1,\r\n" + " \"b\": 2,\r\n" + " \"c\": 3,\r\n"
+ " \"date\": \"31-07-2020\"\r\n" + "}";
assertThatJson(json1).whenIgnoringPaths("date").isEqualTo(json2);
Static Import :
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
Dependency :
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-assertj</artifactId>
<version>2.18.1</version>
<scope>test</scope>
</dependency>
You will have to add json.org or Jackson 1.x or Jackson 2.x or Johnzon or Gson to the classpath as well
I use Jackson
You can transform it to the Json object and delete the unwanted key. Follow the link for details: Remove key from a Json inside a JsonObject
I found out that rest-assured has some interesting functionalities.
You could do:
#Test
public void test() {
get("/xxxx").then().statusCode(200).assertThat()
.body("a", equalTo(1));
}
More info here
You can transform the JSON into a JS object, then compare each property of that object if that property has a key not equal to "date"
In the code below is comparing obj1 to obj2 and ignoring the date property.
It prints "identical" if they are both the same and "not identical" if they are not (ignoring the date property)
var obj1 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"current_date"}' );
var obj2 = JSON.parse ('{ "a":"1","b":"2", "c":"3", "date":"another_date"}' );
let s1 = Object.keys(obj1).length; // length of obj1
let s2 = Object.keys(obj2).length; // length of obj2
let identical = true ;
for ( let i = 0 ; i < s1 ; i ++ ){
if (i >= s2) {
identical = false ;
break ;
}
let current_key = Object.keys(obj1)[i];
let current_value = obj1[current_key];
if (current_key.localeCompare("date") != 0){
if (current_value.localeCompare(obj2[current_key]) != 0){
identical = false ;
break;
}
}
}
if (identical){
console.log ("Identical");
}else {
console.log ("Not identical");
}
Use AssertJ ignoring fields in the comparison
For than it's needed to deserialize json from response (for Spring Test: MvcResult.getResponse().getContentAsString()) to Object.
It is possible to deserialize json response with REST Assured Content-Type based Deserialization
Use unstrict compare with JSONassert.
In order to not creating json by hand, it is possible serialize expected object with Jackson ObjectMapper. For fields exclusion:
Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
map.remove(...);
objectMapper.writeValueAsString(obj)
Using gson how can I return the first and last element from my json so I get the data in this format?
System.out.println("Student: BobGoblin - result: 59");
I have tried this, but it still returns the full JSON object
JsonObject jsonObject = new Gson().fromJson(content.toString(), JsonObject.class);
return jsonObject.get(domain) + " - " + jsonObject.get(result.toString());
First of all: toJson converts something to json. You want to convert json to some kind of object. So use fromJson instead.
Second build an object where you can put that data into. There are plenty examples on the manual site for gson: https://github.com/google/gson/blob/master/UserGuide.md
Let me code that for you. It's not that hard:
import java.util.Map;
import com.google.gson.Gson;
public class GsonTest {
public static void main(String args[]) {
Gson gson = new Gson();
String json = "{\"name\":\"Bog\", \"foo\":\"bar\", \"result\": 59}";
// Using a map
#SuppressWarnings( "unchecked" )
Map<String,Object> map = gson.fromJson( json, Map.class );
System.out.println( "Name: " + map.get( "name" ) + " result: " + map.get( "result" ) );
// Better: using an object
Student student = gson.fromJson( json, Student.class );
System.out.println( "Name: " + student.name + " result: " + student.result );
}
public static class Student {
public String name;
public String foo;
public int result;
}
}
which will result in:
Name: Bog result: 59.0
Name: Bog result: 59
The general Method is: Take the json String and put it in some kind of java object. Then access that java object to get to your data.
Note that you get more control over the kind of data you will receive using the second method. Since json doesn't specify the datatype the parser guesses float/double for age while it uses int in the second example because the class said so.
I have the following json file
[{
"en": {
"key1": "Ap",
"key2": "ap2"
}
},
{
"ar": {
"key1": "Ap",
"key2": "ap2"
}
}
]
I would like to create a Map in Java such as the key is the language (like en or ar) and the value is a object. Something like this.
public class Category {
private String key1;
private String key2;
}
Type type = new TypeToken<Map<String, Category>>() {}.getType();
Gson gson = new Gson();
InputStream in = MyClass.class.getResourceAsStream("/categories.json");
String text = IOUtils.toString(in, StandardCharsets.UTF_8);
Map<String, Category> map = gson.fromJson(text, type);
But when I run this code, I get errors:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $[0]
Is my Json structure wrong or is there an easier way to map this?
try to read this json file
{
"ar": {
"key1": "Ap",
"key2": "ap2"
},
"en": {
"key1": "Ap",
"key2": "ap2"
}
}
The above json is collection of JsonObject like list or array, so just parse it to List of Map objects
Type type = new TypeToken<List<Map<String, Category>>>() {}.getType();
Your json is a list of maps, not only maps. So you have to add it to the type declared.
Try this:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class Category {
private String key1;
private String key2;
#Override
public String toString() {
return "Category{" +
"key1='" + key1 + '\'' +
", key2='" + key2 + '\'' +
'}';
}
public static void main(String[] args) {
String json = "[{\n" +
" \"en\": {\n" +
" \"key1\": \"Ap\",\n" +
" \"key2\": \"ap2\"\n" +
" }\n" +
" },\n" +
"\n" +
" {\n" +
" \"ar\": {\n" +
" \"key1\": \"Ap\",\n" +
" \"key2\": \"ap2\"\n" +
" }\n" +
" }\n" +
"]";
Type type = new TypeToken<List<Map<String, Category>>>() {}.getType();
Gson gson = new Gson();
List<Map<String, Category>> maps = gson.fromJson(json, type);
System.out.println(maps);
}
}
Your input is an json array with two objects. However your target variable 'type' is a of type Object and not an 'Array of Objects'. In simpler terms, Map cannot store an
Array.
Lets take a simpler approach to this problem(not a recommended approach). If we convert the map manually to an array of maps, that would look like this:
yourJson -> [Map1(en,category1(Ap,ap2)),Map2(en,category2(Ap,ap2))]
i.e. An array of Maps
So in java equivalent this becomes:
Type typeOfT2 = new TypeToken<ArrayList<HashMap<String, Category>>>() {}.getType();
List<HashMap<String, Category>> list = gson.fromJson(text, typeOfT2);
We get to what we want, but there are better ways of doing this. We need Jackson instead of Gson for this.(Some one may add a Gson based solution, pretty sure a cleaner one than above exists). Here we will use ObjectMapper from com.fasterxml.jackson.databind.ObjectMapper
ObjectMapper om = new ObjectMapper();
List<Map.Entry<String, Category>> listx = om.readValue(text, ArrayList.class);
If you print listx. You can see this(overridden toString() of Category class):
[{en={key1=Ap, key2=ap2}}, {ar={key1=Ap, key2=ap2}}]
listx is the most accurate representation of your json and not a Map.
Now if you need a map, I will leave that as an exercise for you about how to convert your List of Map.Entry to a Map implementation.
PS.: First long answer here. Apologies for any mistakes.
I have a simple JSON object I wish to parse in Play, I am currently trying the following but having no luck:
HashMap<String,Object> result = new ObjectMapper().readValue(stringBuilder.toString(), HashMap.class);
My JSON Object looks like the following:
[{"id":"537b4f2e30047c51863094dd","from":"jacob","to":"duncan","subject":"Welcome to the message system!","message":"Hello World"},{"id":"537bb23930044f26cfd24464","from":"jacob","to":"duncan","subject":"Welcome to the message system!","message":"Hello World"}]
Can anybody provide an example on how to parse and iterate over this?
Play 2 uses Jackson API for JSON, so you should use it
Sample:
String jsonString = "[{\"id\":\"537b4f2e30047c51863094dd\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"},{\"id\":\"537bb23930044f26cfd24464\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"}]";
JsonNode node = Json.parse(jsonString);
if (node.isArray()) {
Iterator<JsonNode> elements = node.elements();
while (elements.hasNext()) {
JsonNode obj = elements.next();
debug(
"Message with ID: " + obj.get("id")
+ " from: " + obj.get("from")
+ " to: " + obj.get("to")
+ " subject: " + obj.get("subject")
+ " message: " + obj.get("message")
);
}
}
Tip: It was refactored some time ago, so depending on used Play version check Codehaus Jackson or FasterXML Jackson APIs
It looks like you've got a list, where each entry is a map key value pairs.
You can use a standard json parser to convert it into an object like this:
String json = "[{\"id\":\"537b4f2e30047c51863094dd\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"},{\"id\":\"537bb23930044f26cfd24464\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"}]";
Type listType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> data = new Gson().fromJson(json, listType);
Then you can iterate over the List and each Map as normal:
for (Map<String, Object> map : data) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
// do stuff
}
}
P.S.
It looks like all your value data is also in String form, so you might want to consider making a Map<String, String> instead of Map<String, Object> if that's actually the case.