I want to map multiple records in ndJson with my DTO class. Following is my ndJson file:
// Record # 1 (This line is not included in the file and is only for clarification)
{
"profile":{
"salutation":"Mr",
"title":null,
"company":null
},
"phone":{
"home_phone":null
},
"addresses":[
{
"address_id":"1",
"title":"",
"company":null,
"salutation":null,
"first_name":"Veronica",
"last_name":"Costello",
"second_name":null
}
],
"orders":{
"placed_orders_count":2,
"0":{
"order_id":"000000001",
"order_date":"2019-03-27 14:25:03"
},
"1":{
"order_id":"000000002",
"order_date":"2019-03-27 14:25:03"
}
},
"customs":[
]
}
// Record # 2 (This line is not included in the file and is only for clarification)
{
"profile":{
"salutation":null,
"title":null,
"company":null,
"job_title":null
},
"phone":{
"home_phone":null,
"business_phone":null,
"mobile_phone":null,
"fax_number":null
},
"addresses":[
{
"address_id":"2",
"title":""
}
],
"orders":{
"placed_orders_count":0
},
"customs":[
]
}
//Similarly the file has a lot of records
I want to map all the records but only able to map first record.
I have asked a similar question at How to Read ndJSON file in Java but I am not able to map all records with the accepted solution. Below is my code:
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader("customer.json"));
CustomerFeedDTO customerFeedDTO = gson.fromJson(reader, CustomerFeedDTO.class);
And Customer DTO class is:
private Map<String, ?> profile;
private Map<String, ?> phone;
private ArrayList<?> addresses;
private Map<String, ?> orders;
private ArrayList<?> customs;
// Getters and setter
But I am only getting the first record with this code.
How can I map all the records into CustomerDTO object?
You can use reader.peek() method and apply loop on your code such that you need to make list of CustomerDTO and keep on adding object in it like:
List<CustomerFeedDTO> customerFeedDTOs = new ArrayList<CustomerFeedDTO>();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader("customer.json"));
// Required
reader.setLenient(true);
while (reader.peek() != JsonToken.END_DOCUMENT) {
CustomerFeedDTO customerFeedDTO = gson.fromJson(reader, CustomerFeedDTO.class);
customerFeedDTOs.add(customerFeedDTO);
}
Note that reader also has a hasNext() method but that gives Exception when you reach at the end of the document. For further details about this, refer to https://stackoverflow.com/a/52250761/16437679
I have a json
{
"yes":
{
"en": "Yes",
"de": "Ja"
},
"no":
{
"en": "No",
"de": "Nein"
}
}
I want a java function using jackson that can find the highest key possible for a particular json value.
For Instance, if I passed value as Nein -> then no Key which is at the top level should be the output. How can I accomplish this in java??
Well, you could try something like following:
public static void getRootNodeOfJSONObject() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"yes\":{\"en\": \"Yes\",\"de\": \"Ja\"},\"no\": {\"en\": \"No\",\"de\": \"Nein\"}}";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);
for (Iterator key = jsonNodeRoot.fields(); key.hasNext();) {
String text = key.next().toString();
if(text.contains("Nein"))
{
String rootElement = text.substring(0, text.indexOf("="));
System.out.println("Root element: " + rootElement);
}
}
}
public static void main(String[] args) throws IOException {
getRootNodeOfJSONObject();
}
You can try JsonPath (https://github.com/json-path/JsonPath).
Focus on "Predicates" section.
If you need any help let me know.
I am trying to parse my JSONObject to get my JSON Array's data.
But the problem is JSONParser is a class in org.json.simple.JSONParser and the JSONObject is in org.json.JSONObject.
I cannot find any parser in org.json to avoid class cast exception!
Do we have any other way to get this stuff sorted...?
Or am i going in a completely wrong direction?
Please suggest
My JSON looks like :
{
"dataIntents": [
{
"intent": "muster.policy.daily",
"expr": "Am I supposed to register my attendance daily?"
},
{
"intent": "leave.probation",
"expr": "An employee is eligible for how many leaves in 1st year ??"
},
{
"intent": " leave.resigned ",
"expr": "Are resigned employees eligible for pro rata leave credit"
},
{
"intent": " muster.deadline.submission ",
"expr": "By when should I get my pending leave/Emuster applications
approved?"
}
]
}
My main class:
public class DLMain {
public static void main(String[] args) {
try {
JSONParser parser = new JSONParser();
Object obj =
parser.parse(newFileReader("/home/cmss/Downloads/data.json"));
org.json.JSONObject dataObject = (org.json.JSONObject)obj;
System.out.println(dataObject);
org.json.JSONArray getArray =
dataObject.getJSONArray("dataIntents");
for (int i = 0; i < getArray.length(); i++) {
org.json.JSONObject objects = getArray.getJSONObject(i);
String a = objects.getString("expr");
}
System.out.println();
} catch (Exception e) {
System.out.println(e);
}
}
}
I want all values of my "expr" key in a JSONObject or String.
Help appreciated in advance :)
Why don't you use a POJO for that?
So far your Json is a list of intents, you can have something like:
public class Intents {
private List<Intent> dataIntents;
}
and another
public class Intent {
private String intent;
private String expr;
}
(please generate the constructor and getter setter)
And then you can use directly the ObjectMapper and you avoid all the ugly JSON parsing.
Hope it helps!
I have a return from the server which can come through as:
[{
"id":"1",
"objectOne": {
"name":"jim"
}
}, {
"id":"1",
"objectOne": [{
"name": "jim1"
}, {
"name": "jim2"
}
}, {
"id":"1",
"objectOne": null
}]
That is, one value can either be an object, an object array, or null.
I'm using Gson converter with Retrofit and I'm using this TypeAdapterFactory to force single objects to be read as an array:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapterFactory(new ObjectToArrayFactory());
Gson gson = gsonBuilder.create();
Factory:
private class ObjectToArrayAdapter<T> extends TypeAdapter<List<T>> {
Gson gson;
private Class<T> adapterclass;
public ObjectToArrayAdapter(Gson gson, Class<T> adapterclass) {
this.gson = gson;
this.adapterclass = adapterclass;
}
#Override
public void write(JsonWriter out, List<T> value) throws IOException {}
public List<T> read(JsonReader reader) throws IOException {
List<T> list = new ArrayList<T>();
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
// If it's meant to be an array and instead it's a single object, add it to a newly created list.
parseObject(list, reader, gson);
} else if (reader.peek() == JsonToken.BEGIN_ARRAY) {
// Otherwise, if it is actually a list, manually parse each item and add it to the list
parseArray(list, reader, gson);
} else if(reader.peek() == JsonToken.NULL) {
// However if the server gives a null object, just return null.
return null;
}
return list;
}
private void parseArray(List<T> list, JsonReader reader, Gson gson) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
parseObject(list, reader, gson);
}
reader.endArray();
}
private void parseObject(List<T> list, JsonReader reader, Gson gson) throws IOException {
T inning = gson.fromJson(reader, adapterclass);
list.add(inning);
}
}
My problem is that, when I ask Retrofit to parse the value as an Array:
private List<PaymentsOption> objectOne;
The Gson parser seems to get confused, when it get's to the section of the json which looks like this:
"objectOne": null
I've debugged and logged my way through the parsing and it seems it follows what amounts to this code path (For brevity, I've parse out the actual code):
if(reader.peek() == JsonToken.BEGIN_ARRAY) {
reader.beginArray();
while(reader.hasNext()) { // public void parseTag()
if(reader.peek() == JsonToken.BEGIN_OBJECT) {
T inning = gson.fromJson(reader, adapterclass); <-- Crashes here
}
}
reader.endArray();
}
So, it shouldn't be "peeking" as a beginArray as it's "null". It also shouldn't allow a reader.beginArray() as it's still "null". It should peek again and see beginObject. It allows a reader.beginObject() inside of gson.fromJson but fails on reader.readName() as it's actually reading "null". Exception is as follows:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a name but was NULL at line 24 column 39 path $[1].objectOne
10-27 12:05:20.452 E/Exception: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
10-27 12:05:20.452 E/Exception: at com.google.gson.Gson.fromJson(Gson.java:810)
10-27 12:05:20.452 E/Exception: at uk.co.utils.network.ObjectToArrayFactory$ObjectToArrayAdapter.parseTag(ObjectToArrayFactory.java:70)
I don't understand why the reader.peek() is showing first a beginArray, allowing a reader.beginArray(), then showing a reader.peek() as a beginObject() and why it's allowing a reader.beginObject(). As far as I understand, it should have shown a reader.peek() == Json.Token.NULL ...?
You need to write a TypeAdapter and register that when you are building your gson object. In your adapter's read method you can check whether the given parameter is either null or not, or empty and take action accordingly.Your read method will look like:
public Number read(JsonReader in) throws IOException{
if(in.peek() == JsonToken.NULL) in.nextNull();
try{
//read value and take suitable action
}catch(Exception e){}
}
But you need to write a typeAdapter for every different data type that needs special treatment.
Inshort : I am trying to find some api that could just change the value by taking first parameter as jsonString , second parameter as JSONPath and third will be new value of that parameter. But, all I found is this..
https://code.google.com/p/json-path/
This api allows me to find any value in JSON String. But, I am not finding easy way to update the value of any key. For example, Here is a book.json.
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99,
"isbn":"0-553-21311-3"
}
],
"bicycle":{
"color":"red",
"price":19.95
}
}
}
I can access color of bicycle by doing this.
String bicycleColor = JsonPath.read(json, "$.store.bicycle.color");
But I am looking for a method in JsonPath or other api some thing like this
JsonPath.changeNodeValue(json, "$.store.bicycle.color", "green");
String bicycleColor = JsonPath.read(json, "$.store.bicycle.color");
System.out.println(bicycleColor); // This should print "green" now.
I am excluding these options,
Create a new JSON String.
Create a JSON Object to deal with changing value and convert it back to jsonstring
Reason: I have about 500 different requests for different types of service which return different json structure. So, I do not want to manually create new JSON string always. Because, IDs are dynamic in json structure.
Any idea or direction is much appreciated.
Updating this question with following answer.
Copy MutableJson.java.
copy this little snippet and modify as per you need.
private static void updateJsonValue() {
JSONParser parser = new JSONParser();
JSONObject jsonObject = new JSONObject();
FileReader reader = null;
try {
File jsonFile = new File("path to book.json");
reader = new FileReader(jsonFile);
jsonObject = (JSONObject) parser.parse(reader);
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
Map<String, Object> userData = null;
try {
userData = new ObjectMapper().readValue(jsonObject.toJSONString(), Map.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MutableJson json = new MutableJson(userData);
System.out.println("Before:\t" + json.map());
json.update("$.store.book[0].author", "jigish");
json.update("$.store.book[1].category", "action");
System.out.println("After:\t" + json.map().toString());
}
Use these libraries.
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.codehaus.jackson.map.ObjectMapper;
The thing is that the functionality you want is already an undocumented feature of JsonPath. Example using your json structure:
String json = "{ \"store\":{ \"book\":[ { \"category\":\"reference\", \"author\":\"Nigel Rees\", \"title\":\"Sayings of the Century\", \"price\":8.95 }, { \"category\":\"fiction\", \"author\":\"Evelyn Waugh\", \"title\":\"Sword of Honour\", \"price\":12.99, \"isbn\":\"0-553-21311-3\" } ], \"bicycle\":{ \"color\":\"red\", \"price\":19.95 } } }";
DocumentContext doc = JsonPath.parse(json).
set("$.store.bicycle.color", "green").
set("$.store.book[0].price", 9.5);
String newJson = new Gson().toJson(doc.read("$"));
Assuming that parsed JSON can be represented in memory as a Map, you can build an API similar to JsonPath that looks like:
void update(Map<String, Object> json, String path, Object newValue);
I've quickly done a gist of a dirty implementation for simple specific paths (no support for conditions and wildcards) that can traverse json tree, E.g. $.store.name, $.store.books[0].isbn. Here it is: MutableJson.java. It definitely needs improvement, but can give a good start.
Usage example:
import java.util.*;
public class MutableJson {
public static void main(String[] args) {
MutableJson json = new MutableJson(
new HashMap<String, Object>() {{
put("store", new HashMap<String, Object>() {{
put("name", "Some Store");
put("books", Arrays.asList(
new HashMap<String, Object>() {{
put("isbn", "111");
}},
new HashMap<String, Object>() {{
put("isbn", "222");
}}
));
}});
}}
);
System.out.println("Before:\t" + json.map());
json.update("$.store.name", "Book Store");
json.update("$.store.books[0].isbn", "444");
json.update("$.store.books[1].isbn", "555");
System.out.println("After:\t" + json.map());
}
private final Map<String, Object> json;
public MutableJson(Map<String, Object> json) {
this.json = json;
}
public Map<String, Object> map() {
return json;
}
public void update(String path, Object newValue) {
updateJson(this.json, Path.parse(path), newValue);
}
private void updateJson(Map<String, Object> data, Iterator<Token> path, Object newValue) {
Token token = path.next();
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (!token.accept(entry.getKey(), entry.getValue())) {
continue;
}
if (path.hasNext()) {
Object value = token.value(entry.getValue());
if (value instanceof Map) {
updateJson((Map<String, Object>) value, path, newValue);
}
} else {
token.update(entry, newValue);
}
}
}
}
class Path {
public static Iterator<Token> parse(String path) {
if (path.isEmpty()) {
return Collections.<Token>emptyList().iterator();
}
if (path.startsWith("$.")) {
path = path.substring(2);
}
List<Token> tokens = new ArrayList<>();
for (String part : path.split("\\.")) {
if (part.matches("\\w+\\[\\d+\\]")) {
String fieldName = part.substring(0, part.indexOf('['));
int index = Integer.parseInt(part.substring(part.indexOf('[')+1, part.indexOf(']')));
tokens.add(new ArrayToken(fieldName, index));
} else {
tokens.add(new FieldToken(part));
}
};
return tokens.iterator();
}
}
abstract class Token {
protected final String fieldName;
Token(String fieldName) {
this.fieldName = fieldName;
}
public abstract Object value(Object value);
public abstract boolean accept(String key, Object value);
public abstract void update(Map.Entry<String, Object> entry, Object newValue);
}
class FieldToken extends Token {
FieldToken(String fieldName) {
super(fieldName);
}
#Override
public Object value(Object value) {
return value;
}
#Override
public boolean accept(String key, Object value) {
return fieldName.equals(key);
}
#Override
public void update(Map.Entry<String, Object> entry, Object newValue) {
entry.setValue(newValue);
}
}
class ArrayToken extends Token {
private final int index;
ArrayToken(String fieldName, int index) {
super(fieldName);
this.index = index;
}
#Override
public Object value(Object value) {
return ((List) value).get(index);
}
#Override
public boolean accept(String key, Object value) {
return fieldName.equals(key) && value instanceof List && ((List) value).size() > index;
}
#Override
public void update(Map.Entry<String, Object> entry, Object newValue) {
List list = (List) entry.getValue();
list.set(index, newValue);
}
}
A JSON string can be easily parsed into a Map using Jackson:
Map<String,Object> userData = new ObjectMapper().readValue("{ \"store\": ... }", Map.class);
Just answering for folks landing on this page in future for reference.
You could consider using a Java implementation of jsonpatch. RFC can be found here
JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. When used in combination with the HTTP PATCH method it allows partial updates for HTTP APIs in a standards compliant way.
You can specify the operation that needs to be performed (replace, add....), json path at which it has to be performed, and the value which should be used.
Again, taking example from the RFC :
[
{ "op": "test", "path": "/a/b/c", "value": "foo" },
{ "op": "remove", "path": "/a/b/c" },
{ "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
{ "op": "replace", "path": "/a/b/c", "value": 42 },
{ "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
{ "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
]
For Java implementation, I have not used it myself, but you can give a try to https://github.com/fge/json-patch
So in order to change a value within a JSon string, there are two steps:
Parse the JSon
Modify the appropriate field
You are trying to optimize step 2, but understand that you are not going to be able to avoid step 1. Looking at the Json-path source code (which, really, is just a wrapper around Jackson), note that it does do a full parse of the Json string before being able to spit out the read value. It does this parse every time you call read(), e.g. it is not cached.
I think this task is specific enough that you're going to have to write it yourself. Here is what I would do:
Create an object that represents the data in the parsed Json string.
Make sure this object has, as part of it's fields, the Json String pieces that you do not expect to change often.
Create a custom Deserializer in the Json framework of your choice that will populate the fields correctly.
Create a custom Serializer that uses the cached String pieces, plus the data that you expect to change
I think the exact scope of your problem is unusual enough that it is unlikely a library already exists for this. When a program receives a Json String, most of the time what it wants is the fully deserialized object - it is unusual that it needs to FORWARD this object on to somewhere else.