how can i create a JSON string from Json array of objects like below in Java using JSON object
{
header: [
{
"key" : "numberOfRecords",
"value" : "122"
"valueDataType" : "string"
},
{
"key" : "g_udit"
"value" : "1"
"valueDataType" : "string"
},
{
"key": "userNameId"
"value" : "155"
"valueDataType : "string"
}
]
}
expected JSON output requires only values
{
header :
{
"numberOfRecords" : "122",
"g_udit" : "1",
"userNameId" : "155"
}
}
Use JSON query language to transform the JSON structure. A single Josson query statement can do the job.
https://github.com/octomix/josson
Josson josson = Josson.fromJsonString(
"{" +
" \"header\": [" +
" {" +
" \"key\" : \"numberOfRecords\"," +
" \"value\" : \"122\"," +
" \"valueDataType\" : \"string\"" +
" }," +
" { " +
" \"key\" : \"g_udit\"," +
" \"value\" : \"1\"," +
" \"valueDataType\" : \"string\"" +
" }," +
" {" +
" \"key\": \"userNameId\"," +
" \"value\" : \"155\"," +
" \"valueDataType\" : \"string\"" +
" }" +
" ]" +
"}");
JsonNode node = josson.getNode("map(header.map(key::value).mergeObjects())");
Output
{
"header" : {
"numberOfRecords" : "122",
"g_udit" : "1",
"userNameId" : "155"
}
}
First of all you should use any json framework to read and write files. You can use jacskon-utils to use Jackson and make it much simpler to use.
Then you have to define the data classes for input and output types. And finally, convert the data.
#Getter
class InputData {
#JsonProperty("header")
private List<Header> headers;
#Getter
public static class Header {
private String key;
private String value;
private String valueDataType;
}
}
#Setter
class OutputData {
#JsonProperty("header")
private Map<String, String> headers;
}
public static void main(String... args) throws Exception {
InputData inputData = readData(new File("c:/in.json"));
OutputData outputData = createOutputData(inputData);
writeData(new File("c:/out.json"), outputData);
}
private static InputData readData(File file) throws Exception {
try (InputStream in = new FileInputStream(file)) {
return JacksonUtils.readValue(in, InputData.class);
}
}
private static void writeData(File file, OutputData outputData) throws Exception {
try (OutputStream out = new FileOutputStream(file)) {
JacksonUtils.prettyPrint().writeValue(outputData, out);
}
}
private static OutputData createOutputData(InputData inputData) {
Map<String, String> headers = new LinkedHashMap<>();
inputData.getHeaders().forEach(header -> headers.put(header.getKey(), header.getValue()));
OutputData outputData = new OutputData();
outputData.setHeaders(headers);
return outputData;
}
Related
The goal is replace a value in a nested JSON.
Original JSON :
{
"data": {
"car": {
"xia": [
"a0.c904.b0"
]
}
}
}
Expected JSON:
{
"data": {
"car": {
"xia": [
"a0.c234.b0"
]
}
}
}
My code below gives me the JSONElement but I don't know how to put it back to the json object?
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
String inputJson = "{\n"
+ " \"data\": {\n"
+ " \"car\": {\n"
+ " \"xia\": [\n"
+ " \"a0.c904.b0\"\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ "}";
JsonObject jsonObject = new JsonParser().parse(inputJson).getAsJsonObject();
JsonElement jsonElement = jsonObject.get("data").getAsJsonObject().get("car").getAsJsonObject().get("xia");
String str = jsonElement.getAsString();
System.out.println(str);
String[] strs = str.split("\\.");
String replaced = strs[0] + "." + strs[1].replaceAll("\\d+", "234") + "." + strs[2];
System.out.println(replaced);
JsonElement jsonElementReplaced = new JsonParser().parse(replaced);
I just had to do :
jsonObject.get("data").getAsJsonObject().get("car").getAsJsonObject().add("xia", jsonElementReplaced);
I am trying to add specific values from the following JSON to a Java ArrayList. I would then like to use this ArrayList within a JSP. This is the JSON:
{
"page": 1,
"rpp": 3,
"total": 3294,
"request_time": "2018-04-23T16:10:20+01:00",
"stops": [
{
"atcocode": "370023715",
"longitude": -1.46616,
"latitude": 53.38248,
"distance": 57
},
{
"atcocode": "370027281",
"longitude": -1.46583,
"latitude": 53.38228,
"distance": 77
},
{
"atcocode": "370022803",
"longitude": -1.46616,
"latitude": 53.38227,
"distance": 80
}
]
}
I would like to add each longitude and latitude elements from under the "stops" subtree into 2 different ArrayLists. This is my attempted code for that:
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
try {
String json = readUrl (link);
JsonParser parser = new JsonParser();
JsonElement element = parser . parse (json);
if (element.isJsonObject()) {
JsonObject bus = element . getAsJsonObject ();
JsonArray array = bus . getAsJsonArray ("stops");
for (int i = 0; i < array.size(); i++) {
List<String> longitudes = new ArrayList<String>();
List<String> latitudes = new ArrayList<String>();
longitudes.add(array.get(i)).get("longitude");
latitudes.add(array.get(i)).get("latitude");
request.setAttribute("longitudes", longitudes);
request.setAttribute("latitudes", latitudes);
RequestDispatcher dispatcher = request . getRequestDispatcher ("latlong.jsp");
dispatcher.forward(request, response);
}
}
}
}
i get the following error of: "error: incompatible types: JsonElement cannot be converted to String"
Thank you in advance!
One other error you have is that the longitudes, latitudes lists are inside of the loop.
Here is a simple testable piece of code which extracts the data from the JSON and can be tested locally. You can adapt it to your purposes ...
public static void main(String[] args) {
String json = "{\n" +
"\"page\": 1,\n" +
"\"rpp\": 3,\n" +
"\"total\": 3294,\n" +
"\"request_time\": \"2018-04-23T16:10:20+01:00\",\n" +
"\"stops\": [\n" +
"{\n" +
" \"atcocode\": \"370023715\",\n" +
" \"longitude\": -1.46616,\n" +
" \"latitude\": 53.38248,\n" +
" \"distance\": 57\n" +
"},\n" +
"{\n" +
" \"atcocode\": \"370027281\", \n" +
" \"longitude\": -1.46583,\n" +
" \"latitude\": 53.38228,\n" +
" \"distance\": 77\n" +
"},\n" +
"{\n" +
" \"atcocode\": \"370022803\",\n" +
" \"longitude\": -1.46616,\n" +
" \"latitude\": 53.38227,\n" +
" \"distance\": 80\n" +
" }\n" +
"]\n" +
"}";
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
List<String> longitudes = new ArrayList<>();
List<String> latitudes = new ArrayList<>();
if (element.isJsonObject()) {
JsonObject bus = element . getAsJsonObject ();
JsonArray array = bus.getAsJsonArray("stops");
array.forEach(jsonElement -> {
extractToList(longitudes, (JsonObject) jsonElement, "longitude");
extractToList(latitudes, (JsonObject) jsonElement, "latitude");
});
}
System.out.println(longitudes);
System.out.println(latitudes);
}
private static void extractToList(List<String> list, JsonObject jsonElement, String field) {
final JsonElement longitude = jsonElement.get(field);
if(longitude != null) {
list.add(longitude.getAsString());
}
}
If you run this you get printed out on the console:
[-1.46616, -1.46583, -1.46616]
[53.38248, 53.38228, 53.38227]
I have assumed you are using Google's GSON library.
Instead of
longitudes.add(array.get(i)).get("longitude");
latitudes.add(array.get(i)).get("latitude");
Use
longitudes.add(array.get(i).get("longitude").getAsString());
latitudes.add(array.get(i).get("latitude").getAsString());
I am getting a JSON object which looks like:
{
"id": "1",
"name": "Hw",
"price": {
"value": "10"
},
{
"items": [{
"id": "1"
}]
}
}
I want to represent this as flat map, but I want to represent the array of items as a list.
My output should look like:
{
"id": "1",
"name":"Hw",
"price":"10",
"items": ["1"]
}
Can anybody suggest me how I can achieve this? I tried this approach:
How to deserialize JSON into flat, Map-like structure?
Output from the above tried link:
{
"id": "1",
"name":"Hw",
"price.value":"10",
"items[0].id": "1"
}
But it is representing the arrays values as array[0], array[1] which I don't need. I need this array as a list.
The JSON you've given is not valid. I assume it's:
{
"id": "1",
"name": "Hw",
"price": {
"value": "10"
},
"items": [{
"id": "1"
}]
}
There cannot be a generic solution to what you're asking. But for this particular JSON, this will do(using json-simple):
#SuppressWarnings("unchecked")
public Map<String, String> transform(String inputJSON) throws ParseException {
Map<String, String> result = new LinkedHashMap<>();
JSONObject inputJSONObj = (JSONObject) new JSONParser().parse(inputJSON);
String id = inputJSONObj.getOrDefault("id", "").toString();
String name = inputJSONObj.getOrDefault("name", "").toString();
String price = ((JSONObject) inputJSONObj.getOrDefault("price", new JSONObject())).getOrDefault("value", "")
.toString();
JSONArray itemsArray = (JSONArray) inputJSONObj.getOrDefault("items", new JSONArray());
int n = itemsArray.size();
String[] itemIDs = new String[n];
for (int i = 0; i < n; i++) {
JSONObject itemObj = (JSONObject) itemsArray.get(i);
String itemId = itemObj.getOrDefault("id", "").toString();
itemIDs[i] = itemId;
}
result.put("id", id);
result.put("name", name);
result.put("price", price);
result.put("items", Arrays.toString(itemIDs));
return result;
}
An approach for you with Gson. This do exactly what you want " represent this as flat map, but I want to represent the array of items as a list"
public class ParseJson1 {
public static void main (String[] args){
Type type = new TypeToken<HashMap<String, Object>>() {
}.getType();
Gson gson = new Gson();
String json = "{\n" +
" \"id\": \"1\",\n" +
" \"name\": \"Hw\", \n" +
" \"price\": {\n" +
" \"value\": \"10\"\n" +
" },\n" +
" \"items\": [{\n" +
" \"id\": \"1\"\n" +
" }]\n" +
" }\n";
HashMap<String, Object> map = gson.fromJson(json, type);
Object val = null;
for(String key : map.keySet()){
val = map.get(key);
if(val instanceof List){
for(Object s : (List)val){
System.out.println(key + ":" + s);
}
} else
System.out.println(key + ":" + map.get(key));
}
}
}
you have to convert your String in Map collection Map<String, String> which will help you to convert your Map Array to JSON format.
JSONObject jsonObject = new JSONObject();
Map<String, String> mapObject = new HashMap<String, String>();
mapObject.put("id", "1");
mapObject.put("name", "VBage");
mapObject.put("mobile", "654321");
jsonObject.put("myJSON", mapObject);
System.out.println(jsonObject.toString());
First, the JSON does not seems to have a correct format. Do you mean this?
{
"id": "1",
"name": "Hw",
"price": {
"value": "10"
},
"items": [{
"id": "1"
}]
}
In addition, since you were attaching the link of (How to deserialize JSON into flat, Map-like structure?), I assume you wants to flatten the JSON in the same manner, in which the result should be
{
id=1,
name=Hw,
price.value=10,
items[0]=1,
}
Also, if you just want the item to return a list of id (i.e. "items": ["1"]), then it is more logical to get a JSON of
{
"id": "1",
"name": "Hw",
"price": {
"value": "10"
},
"items": [ "1" ] // instead of "items": [{"id": "1"}]
}
The link that you have attached (How to deserialize JSON into flat, Map-like structure?) provides a general solution without any customization. It shouldn't know that "id" is the value you want to append on items.
Therefore, my first suggestion is to change the JSON to be "items": [ "1" ]
If for any reasons the JSON cannot be changed, then you will need to do some customization, which will be like this:
import org.codehaus.jackson.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.codehaus.jackson.node.ValueNode;
import org.junit.Test;
public class Main {
String json = "{\n" +
" \"id\": \"1\",\n" +
" \"name\": \"Hw\", \n" +
" \"price\": {\n" +
" \"value\": \"10\"\n" +
" },\n" +
" \"items\": [{\n" +
" \"id\": \"1\"\n" +
" }]\n" +
" }\n";
#Test
public void testCreatingKeyValues() {
Map<String, String> map = new HashMap<String, String>();
try {
addKeys("", new ObjectMapper().readTree(json), map);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(map);
}
private void addKeys(String currentPath, JsonNode jsonNode, Map<String, String> map) {
if (jsonNode.isObject()) {
ObjectNode objectNode = (ObjectNode) jsonNode;
Iterator<Map.Entry<String, JsonNode>> iter = objectNode.getFields();
String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";
while (iter.hasNext()) {
Map.Entry<String, JsonNode> entry = iter.next();
// Customization here
if (entry.getKey().equals("items")) {
ArrayNode arrayNode = (ArrayNode) entry.getValue();
for (int i = 0; i < arrayNode.size(); i++) {
addKeys(currentPath + entry.getKey() + "[" + i + "]", arrayNode.get(i).get("id"), map);
}
} else {
addKeys(pathPrefix + entry.getKey(), entry.getValue(), map);
}
}
} else if (jsonNode.isArray()) {
ArrayNode arrayNode = (ArrayNode) jsonNode;
for (int i = 0; i < arrayNode.size(); i++) {
addKeys(currentPath + "[" + i + "]", arrayNode.get(i), map);
}
} else if (jsonNode.isValueNode()) {
ValueNode valueNode = (ValueNode) jsonNode;
map.put(currentPath, valueNode.asText());
}
}
}
Try understanding the format that you need, and then study the above code. It should give you the answer.
I have json array from server response like this.
[
{
"idApp" : "001"
"AppName" : "Test App 1"
},
{
"idApp" : "002"
"AppName" : "Test App 2"
},
{
"idApp" : "003"
"AppName" : "Test App 3"
},
{
"idApp" : "004"
"AppName" : "Test App 4"
}
]
i just want to know the position of this object in jsonarray programatically
{
"idApp" : "003"
"AppName" : "Test App 3"
}
This should work for you
for(int i = 0 ; i < arguments.length(); i++){
if(arguments.getObject(i).get("idApp").asString().equals("003"))
System.out.println("Found it : " + i);
}
You forget , in each object in json string after each data like below :
{
"idApp" : "003",
"AppName" : "Test App 3"
}
By the way to get postion match 003 in idApp we can use Gson library http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1
Add dependency in pom.xml :
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Create modal class like your json object :
public class Modal {
private String idApp;
private String AppName;
public String getIdApp() {
return idApp;
}
public void setIdApp(String idApp) {
this.idApp = idApp;
}
public String getAppName() {
return AppName;
}
public void setAppName(String AppName) {
this.AppName = AppName;
}
}
Now convert json string to array of object and loop over array and find match object like below :
public class JsonUtils {
public static void main(String[] args) {
System.out.println("Position for 003 is = " + new JsonUtils().getPositionFromJsonString());
}
public int getPositionFromJsonString() {
Gson gson = new Gson();
String jsonString = "["
+ " {"
+ " \"idApp\" : \"001\","
+ " \"AppName\" : \"Test App 1\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"002\","
+ " \"AppName\" : \"Test App 2\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"003\","
+ " \"AppName\" : \"Test App 3\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"004\","
+ " \"AppName\" : \"Test App 4\""
+ " }"
+ ""
+ "]";
Modal[] modals = gson.fromJson(jsonString, Modal[].class);
int pos = -1;
for (Modal m : modals) {
pos++;
if ("003".equalsIgnoreCase(m.getIdApp())) {
return pos;
}
}
return -1;
}
}
You can do something like that to remove the specefic jsonObject from jsonArray.
//find the value in your jsonarray
for (int i = 0; i < jsonarray.length; ++i)
{
JSONObject rec =jsonarray.getJSONObject(i);
//check the condition if the given id is associated with the object then remove it
if(rec.getString("idApp").equals("your_passedId")
{
jsonarray.remove(i)
}
}
May be late, but it may help to someone
int index = 0;
while (index < yourArray.length()) {
JSONObject object = yourArray.optJSONObject(index);
if (object != null) {
if (/*your condition is true*/ true){
yourArray.remove(index);
index--;
}
}
index++;
}
Given the following JSON response:
{
"status": "OK",
"regions": [
{
"id": "69",
"name": "North Carolina Coast",
"color": "#01162c",
"hasResorts": 1
},
{
"id": "242",
"name": "North Carolina Inland",
"color": "#01162c",
"hasResorts": 0
},
{
"id": "17",
"name": "North Carolina Mountains",
"color": "#01162c",
"hasResorts": 1
},
{
"id": "126",
"name": "Outer Banks",
"color": "#01162c",
"hasResorts": 1
}
]
}
I'm trying to create a List of Region objects. Here's a very abridged version of my current code:
JSONObject jsonObject = new JSONObject(response);
String regionsString = jsonObject.getString("regions");
Type listType = new TypeToken<ArrayList<Region>>() {}.getType();
List<Region> regions = new Gson().fromJson(regionsString, listType);
This is all working fine. However, I'd like to exclude the regions in the final List that hasResorts == 0. I realize I can loop through the actual JSONObjects and check them before calling fromJSON on each region. But I'm assuming there is a GSON specific way of doing this.
I was looking at the ExclusionStrategy(). Is there a simple way to implement this to JSON deserialization?
ExclusionStrategy won't help you since it works without the context of deserialization. Indeed, you can exclude only a specific kind of class. I think that best way of doing it is through custom deserialization. Here is what I mean (you can copy&paste&try immediately):
package stackoverflow.questions.q19912055;
import java.lang.reflect.Type;
import java.util.*;
import stackoverflow.questions.q17853533.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class Q19912055 {
class Region {
String id;
String name;
String color;
Integer hasResorts;
#Override
public String toString() {
return "Region [id=" + id + ", name=" + name + ", color=" + color
+ ", hasResorts=" + hasResorts + "]";
}
}
static class RegionDeserializer implements JsonDeserializer<List<Region>> {
public List<Region> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
if (json == null)
return null;
ArrayList<Region> al = new ArrayList<Region>();
for (JsonElement e : json.getAsJsonArray()) {
boolean deserialize = e.getAsJsonObject().get("hasResorts")
.getAsInt() > 0;
if (deserialize)
al.add((Region) context.deserialize(e, Region.class));
}
return al;
}
}
/**
* #param args
*/
public static void main(String[] args) {
String json =
" [ "+
" { "+
" \"id\": \"69\", "+
" \"name\": \"North Carolina Coast\", "+
" \"color\": \"#01162c\", "+
" \"hasResorts\": 1 "+
" }, "+
" { "+
" \"id\": \"242\", "+
" \"name\": \"North Carolina Inland\", "+
" \"color\": \"#01162c\", "+
" \"hasResorts\": 0 "+
" }, "+
" { "+
" \"id\": \"17\", "+
" \"name\": \"North Carolina Mountains\", "+
" \"color\": \"#01162c\", "+
" \"hasResorts\": 1 "+
" }, "+
" { "+
" \"id\": \"126\", "+
" \"name\": \"Outer Banks\", "+
" \"color\": \"#01162c\", "+
" \"hasResorts\": 1 "+
" } "+
" ] ";
Type listType = new TypeToken<ArrayList<Region>>() {}.getType();
List<Region> allRegions = new Gson().fromJson(json, listType);
System.out.println(allRegions);
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(listType, new RegionDeserializer());
Gson gson2 = builder.create();
List<Region> regionsHaveResort = gson2.fromJson(json, listType);
System.out.println(regionsHaveResort);
}
}