I'm using json-simple and I need to pretty-print JSON data (make it more human readable).
I haven't been able to find this functionality within that library.
How is this commonly achieved?
Google's GSON can do this in a nice way:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonString);
String prettyJsonString = gson.toJson(je);
or since it is now recommended to use the static parse method from JsonParser you can also use this instead:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement je = JsonParser.parseString(uglyJsonString);
String prettyJsonString = gson.toJson(je);
Here is the import statement:
import com.google.gson.*;
Here is the Gradle dependency:
implementation 'com.google.code.gson:gson:2.8.7'
I used org.json built-in methods to pretty-print the data.
import org.json.JSONObject;
JSONObject json = new JSONObject(jsonString); // Convert text to object
System.out.println(json.toString(4)); // Print it with specified indentation
The order of fields in JSON is random per definition. A specific order is subject to parser implementation.
With Jackson (com.fasterxml.jackson.databind):
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject))
From: How to enable pretty print JSON output (Jackson)
I know this is already in the answers, but I want to write it separately here because chances are, you already have Jackson as a dependency and so all you will need would be an extra line of code
It seems like GSON supports this, although I don't know if you want to switch from the library you are using.
From the user guide:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(someObject);
Using org json. Reference link
JSONObject jsonObject = new JSONObject(obj);
String prettyJson = jsonObject.toString(4);
Using Gson. Reference link
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(obj);
Using Jackson. Reference link
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String json = mapper.writeValueAsString(obj);
Using Genson. Reference link.
Genson prettyGenson = new GensonBuilder().useIndentation(true).create();
String prettyJson = prettyGenson.serialize(obj);
Using javax.json. Reference link.
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory writerFactory = Json.createWriterFactory(config);
Writer writer = new StringWriter();
writerFactory.createWriter(writer).write(jsonObject);
String json = writer.toString();
Using Moshi library. Reference link.
String json = jsonAdapter.indent(" ").toJson(emp1);
(OR)
Buffer buffer = new Buffer();
JsonWriter jsonWriter = JsonWriter.of(buffer);
jsonWriter.setIndent(" ");
jsonAdapter.toJson(jsonWriter, emp1);
json = buffer.readUtf8();
If you are using a Java API for JSON Processing (JSR-353) implementation then you can specify the JsonGenerator.PRETTY_PRINTING property when you create a JsonGeneratorFactory.
The following example has been originally published on my blog post.
import java.util.*;
import javax.json.Json;
import javax.json.stream.*;
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JsonGenerator.PRETTY_PRINTING, true);
JsonGeneratorFactory jgf = Json.createGeneratorFactory(properties);
JsonGenerator jg = jgf.createGenerator(System.out);
jg.writeStartObject() // {
.write("name", "Jane Doe") // "name":"Jane Doe",
.writeStartObject("address") // "address":{
.write("type", 1) // "type":1,
.write("street", "1 A Street") // "street":"1 A Street",
.writeNull("city") // "city":null,
.write("verified", false) // "verified":false
.writeEnd() // },
.writeStartArray("phone-numbers") // "phone-numbers":[
.writeStartObject() // {
.write("number", "555-1111") // "number":"555-1111",
.write("extension", "123") // "extension":"123"
.writeEnd() // },
.writeStartObject() // {
.write("number", "555-2222") // "number":"555-2222",
.writeNull("extension") // "extension":null
.writeEnd() // }
.writeEnd() // ]
.writeEnd() // }
.close();
Pretty printing with GSON in one line:
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(jsonString)));
Besides inlining, this is equivalent to the accepted answer.
My situation is my project uses a legacy (non-JSR) JSON parser that does not support pretty printing. However, I needed to produce pretty-printed JSON samples; this is possible without having to add any extra libraries as long as you are using Java 7 and above:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");
scriptEngine.put("jsonString", jsonStringNoWhitespace);
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)");
String prettyPrintedJson = (String) scriptEngine.get("result");
Most of the existing answers either depend on some external library, or requiring a special Java version. Here is a simple code to pretty print a JSON string, only using general Java APIs (available in Java 7 for higher; haven't tried older version although).
The basic idea is to tigger the formatting based on special characters in JSON. For example, if a '{' or '[' is observed, the code will create a new line and increase the indent level.
Disclaimer: I only tested this for some simple JSON cases (basic key-value pair, list, nested JSON) so it may need some work for more general JSON text, like string value with quotes inside, or special characters (\n, \t etc.).
/**
* A simple implementation to pretty-print JSON file.
*
* #param unformattedJsonString
* #return
*/
public static String prettyPrintJSON(String unformattedJsonString) {
StringBuilder prettyJSONBuilder = new StringBuilder();
int indentLevel = 0;
boolean inQuote = false;
for(char charFromUnformattedJson : unformattedJsonString.toCharArray()) {
switch(charFromUnformattedJson) {
case '"':
// switch the quoting status
inQuote = !inQuote;
prettyJSONBuilder.append(charFromUnformattedJson);
break;
case ' ':
// For space: ignore the space if it is not being quoted.
if(inQuote) {
prettyJSONBuilder.append(charFromUnformattedJson);
}
break;
case '{':
case '[':
// Starting a new block: increase the indent level
prettyJSONBuilder.append(charFromUnformattedJson);
indentLevel++;
appendIndentedNewLine(indentLevel, prettyJSONBuilder);
break;
case '}':
case ']':
// Ending a new block; decrese the indent level
indentLevel--;
appendIndentedNewLine(indentLevel, prettyJSONBuilder);
prettyJSONBuilder.append(charFromUnformattedJson);
break;
case ',':
// Ending a json item; create a new line after
prettyJSONBuilder.append(charFromUnformattedJson);
if(!inQuote) {
appendIndentedNewLine(indentLevel, prettyJSONBuilder);
}
break;
default:
prettyJSONBuilder.append(charFromUnformattedJson);
}
}
return prettyJSONBuilder.toString();
}
/**
* Print a new line with indention at the beginning of the new line.
* #param indentLevel
* #param stringBuilder
*/
private static void appendIndentedNewLine(int indentLevel, StringBuilder stringBuilder) {
stringBuilder.append("\n");
for(int i = 0; i < indentLevel; i++) {
// Assuming indention using 2 spaces
stringBuilder.append(" ");
}
}
Now this can be achieved with the JSONLib library:
http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONObject.html
If (and only if) you use the overloaded toString(int indentationFactor) method and not the standard toString() method.
I have verified this on the following version of the API:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
Following the JSON-P 1.0 specs (JSR-353) a more current solution for a given JsonStructure (JsonObject or JsonArray) could look like this:
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
public class PrettyJson {
private static JsonWriterFactory FACTORY_INSTANCE;
public static String toString(final JsonStructure status) {
final StringWriter stringWriter = new StringWriter();
final JsonWriter jsonWriter = getPrettyJsonWriterFactory()
.createWriter(stringWriter);
jsonWriter.write(status);
jsonWriter.close();
return stringWriter.toString();
}
private static JsonWriterFactory getPrettyJsonWriterFactory() {
if (null == FACTORY_INSTANCE) {
final Map<String, Object> properties = new HashMap<>(1);
properties.put(JsonGenerator.PRETTY_PRINTING, true);
FACTORY_INSTANCE = Json.createWriterFactory(properties);
}
return FACTORY_INSTANCE;
}
}
In JSONLib you can use this:
String jsonTxt = JSONUtils.valueToString(json, 8, 4);
From the Javadoc:
You can use Gson like below
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonString = gson.toJson(object);
From the post JSON pretty print using Gson
Alternatively, You can use Jackson like below
ObjectMapper mapper = new ObjectMapper();
String perttyStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
From the post Pretty print JSON in Java (Jackson)
Hope this help!
Update: new JsonParser().parse(...) is #deprecated
Based on the javadoc for Gson 2.8.6:
No need to instantiate this class, use the static methods instead.
JsonParser static methods:
JsonParser.parseString(jsonString);
JsonParser.parseReader(reader);
Packages:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
Example:
private Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static String getPerfectJSON(String unformattedJSON) {
String perfectJSON = GSON.toJson(JsonParser.parseString(unformattedJSON));
return perfectJSON;
}
Google Gson dependency using Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Reference:
JsonParser is deprecated
This worked for me, using Jackson:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(JSONString)
So I too like the json-simple lib, and looked into pretty printing its output. Unfortunately, while it's an open issue there, I couldn't find any code for it. So I thought I'd give it a try, here's what I came up with (using their own source)..
public class JsonPrinter {
public static String toJson(Map<?,?> map) {
StringBuilder out = new StringBuilder(32);
new JsonPrinter(out).print(map);
return out.toString();
}
public static String toJson(List<?> list) {
StringBuilder out = new StringBuilder(32);
new JsonPrinter(out).print(list);
return out.toString();
}
private final Appendable out;
private final String indentUnit;
private final String newLine;
private int indents;
public JsonPrinter(Appendable out) {
this(out, " ", System.lineSeparator());
}
/**
*
*/
public JsonPrinter(Appendable out, String indentUnit, String newLine) {
this.out = Objects.requireNonNull(out, "null out");
this.indentUnit = Objects.requireNonNull(indentUnit, "null indentUnit");
this.newLine = Objects.requireNonNull(newLine, "null newLine");
if (!indentUnit.isBlank())
throw new IllegalArgumentException(
"indentUnit must be a blank sequence (quoted): '" + indentUnit + "'");
if (!"\r\n".equals(newLine) && ! "\n".equals(newLine))
throw new IllegalArgumentException(
"unrecognized newLine (quoted): '" + newLine + "'");
}
public void print(List<?> list) throws UncheckedIOException {
try {
assert indents == 0;
printImpl(list);
assert indents == 0;
} catch (IOException iox) {
throw new UncheckedIOException("on print(List): " + list, iox);
}
}
public void print(Map<?,?> map) throws UncheckedIOException {
try {
assert indents == 0;
printImpl(map);
assert indents == 0;
} catch (IOException iox) {
throw new UncheckedIOException("on print(Map): " + map, iox);
}
}
protected void printImpl(List<?> list) throws IOException {
if (list == null) {
out.append("null");
return;
}
boolean first = true;
var iter = list.iterator();
open('[');
while (iter.hasNext()) {
if (first)
first = false;
else
out.append(',');
out.append(newLine);
appendIndents();
appendValue(iter.next());
}
close(']');
}
protected void printImpl(Map<?, ?> map) throws IOException {
if (map == null) {
out.append("null");
return;
}
boolean first = true;
var iter = map.entrySet().iterator();
open('{');
while (iter.hasNext()) {
if (first)
first = false;
else
out.append(',');
out.append(newLine);
appendIndents();
var entry = iter.next();
print(entry.getKey().toString(), entry.getValue());
}
close('}');
}
private void open(char c) throws IOException {
out.append(c);
++indents;
}
private void close(char c) throws IOException {
--indents;
out.append(newLine);
appendIndents();
out.append(c);
}
private void appendIndents() throws IOException {
for (int count = indents; count-- > 0; )
out.append(indentUnit);
}
private void print(String key, Object value) throws IOException {
out.append('"');
appendString(key);
out.append('"').append(':').append(' ');
appendValue(value);
}
private void appendString(String s) throws IOException {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch(ch){
case '"':
out.append("\\\"");
break;
case '\\':
out.append("\\\\");
break;
case '\b':
out.append("\\b");
break;
case '\f':
out.append("\\f");
break;
case '\n':
out.append("\\n");
break;
case '\r':
out.append("\\r");
break;
case '\t':
out.append("\\t");
break;
case '/':
out.append("\\/");
break;
default:
//Reference: http://www.unicode.org/versions/Unicode5.1.0/
if ((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')) {
String ss = Integer.toHexString(ch);
out.append("\\u");
for (int k=0; k < 4-ss.length(); k++) {
out.append('0');
}
out.append(ss.toUpperCase());
}
else{
out.append(ch);
}
}
}//for
}
private void appendValue(Object value) throws IOException {
if (value == null) {
out.append("null");
} else if (value instanceof String) {
out.append('"');
appendString(value.toString());
out.append('"');
} else if (value instanceof Double) {
var num = (Double) value;
if (num.isInfinite() || num.isNaN())
out.append("null");
else
out.append(value.toString());
} else if (value instanceof Float) {
var num = (Float) value;
if (num.isInfinite() || num.isNaN())
out.append("null");
else
out.append(value.toString());
} else if (value instanceof Map) {
printImpl((Map<?,?>) value);
} else if (value instanceof List) {
printImpl((List<?>) value);
// } else if (value instanceof Number || value instanceof Boolean) {
// out.append(value.toString());
} else {
out.append(value.toString());
}
}
}
It works for JSONObject and JSONArray even tho it has no dependeny on them.. cuz these are regular Map and List objects resp. (and the fact code was lifted from same lib).
https://github.com/crums-io/io-util/blob/master/src/main/java/io/crums/util/json/JsonPrinter.java
You can use small json library
String jsonstring = ....;
JsonValue json = JsonParser.parse(jsonstring);
String jsonIndendedByTwoSpaces = json.toPrettyString(" ");
I also use the org.json.simple package. I have simply coded the formatter, but since I don't have nulls, numbers or booleans in my JSON objects in the program that I wrote, I only coded for strings, objects and arrays. If anyone is interested, let this just be in the public domain. You are welcome to add the missing data types (where it says in the comment "it's a string"). Also, you can add the indentation as a parameter whereas mine is just two spaces. Please reshare after you've tested your improvements.
Usage: printJsonObject(jsonObject, "");
Functions:
public static void printJsonObject(JSONObject object, String prefix) {
boolean notFirst = false;
System.out.println(prefix + "{");
for (Object key : object.keySet()) {
if (notFirst) {
System.out.println(", ");
}
notFirst = true;
Object value = object.get(key);
System.out.print(prefix + " " + "\"" + key + "\"" + ": ");
if (value instanceof JSONObject) {
printJsonObject((JSONObject) value, prefix + " ");
} else if (value instanceof JSONArray) {
printJsonArray((JSONArray) value, prefix + " ");
} else { // it's a string
System.out.print("\"" + value + "\"");
}
}
System.out.println("");
System.out.print(prefix + "}");
}
public static void printJsonArray(JSONArray array, String prefix) {
boolean notFirst = false;
System.out.println("[");
for (Object item : array) {
if (notFirst) {
System.out.println(", ");
}
notFirst = true;
if (item instanceof JSONObject) {
printJsonObject((JSONObject) item, prefix + " ");
} else if (item instanceof JSONArray) {
printJsonArray((JSONArray) item, prefix + " ");
} else {
System.out.print(prefix + " " + "\"" + item + "\"");
}
}
System.out.println("");
System.out.print(prefix + "]");
}
This would be a public method to print a pretty version of your object (You need the Gson dependency installed:
import com.google.gson.GsonBuilder;
...
public void printMe(){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJSON = gson.toJson(this);
System.out.println(printable);
}
Underscore-java has static method U.formatJson(json).
Five format types are supported: 2, 3, 4, tabs and compact. Live example
import com.github.underscore.U;
import static com.github.underscore.Json.JsonStringBuilder.Step.TABS;
import static com.github.underscore.Json.JsonStringBuilder.Step.TWO_SPACES;
public class MyClass {
public static void main(String args[]) {
String json = "{\"Price\": {"
+ " \"LineItems\": {"
+ " \"LineItem\": {"
+ " \"UnitOfMeasure\": \"EACH\", \"Quantity\": 2, \"ItemID\": \"ItemID\""
+ " }"
+ " },"
+ " \"Currency\": \"USD\","
+ " \"EnterpriseCode\": \"EnterpriseCode\""
+ "}}";
System.out.println(U.formatJson(json, TWO_SPACES));
System.out.println(U.formatJson(json, TABS));
}
}
Output:
{
"Price": {
"LineItems": {
"LineItem": {
"UnitOfMeasure": "EACH",
"Quantity": 2,
"ItemID": "ItemID"
}
},
"Currency": "USD",
"EnterpriseCode": "EnterpriseCode"
}
}
{
"Price": {
"LineItems": {
"LineItem": {
"UnitOfMeasure": "EACH",
"Quantity": 2,
"ItemID": "ItemID"
}
},
"Currency": "USD",
"EnterpriseCode": "EnterpriseCode"
}
}
Related
I'm trying out this code from Microsoft, however I wanted to combine the 2 features they made. One is analyzing image and one is detecting celebrities. However, I'm having a hard time on how will I return 2 values from one function.
Here is the process method...
private String process() throws VisionServiceException, IOException {
Gson gson = new Gson();
String model = "celebrities";
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, output);
ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());
AnalysisResult v = this.client.describe(inputStream, 1);
AnalysisInDomainResult m = this.client.analyzeImageInDomain(inputStream,model);
String result = gson.toJson(v);
String result2 = gson.toJson(m);
Log.d("result", result);
return result, result2;
}
And combine the 2 results with this method...
#Override
protected void onPostExecute(String data) {
super.onPostExecute(data);
mEditText.setText("");
if (e != null) {
mEditText.setText("Error: " + e.getMessage());
this.e = null;
} else {
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
//pang detect ng peymus...
AnalysisInDomainResult result2 = gson.fromJson(data, AnalysisInDomainResult.class);
//decode the returned result
JsonArray detectedCelebs = result2.result.get("celebrities").getAsJsonArray();
if(result2.result != null){
mEditText.append("Celebrities detected: "+detectedCelebs.size()+"\n");
for(JsonElement celebElement: detectedCelebs) {
JsonObject celeb = celebElement.getAsJsonObject();
mEditText.append("Name: "+celeb.get("name").getAsString() +", score" +
celeb.get("confidence").getAsString() +"\n");
}
}else {
for (Caption caption: result.description.captions) {
mEditText.append("Your seeing " + caption.text + ", confidence: " + caption.confidence + "\n");
}
mEditText.append("\n");
}
/* for (String tag: result.description.tags) {
mEditText.append("Tag: " + tag + "\n");
}
mEditText.append("\n");
mEditText.append("\n--- Raw Data ---\n\n");
mEditText.append(data);*/
mEditText.setSelection(0);
}
}
Thanks in advance!
you can use. the parameter are two objects so can you can put everything
final Pair<String, String> pair = Pair.create("1", "2");
String a = pair.first;
String b = pair.second;
Simply use Bundle
Bundle bundle = new Bundle();
bundle.putString("key_one", "your_first_value");
bundle.putString("key_two", "your_second_value");
return bundle;
You can add multiple values with different types in Bundle.In this case your method's return type should be Bundle.
or AbstractMap#SimpleEntry (or as of Java-9 Map#entry)
You can always return two values via Arrays.asList(one, two) also
Here is my enum:
public enum StaffRanks {
HELPER("helpers"),
MODERATOR("moderators"),
ADMINISTRATOR("administrators"),
DEVELOPER("developers"),
OWNER("owners");
private String name;
StaffRanks(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This is how I create my Multimap:
public static Multimap<String, StaffRanks> staffMembers;
public static void fetchPlayerRanks() {
JsonParser parser = new JsonParser();
try {
Object obj = parser.parse(new FileReader(".\\Data\\staff_list.json"));
JsonObject staffList = (JsonObject) obj;
Type listType = new TypeToken<List<String>>() {
}.getType();
staffMembers = ArrayListMultimap.create();
for (StaffRanks rank : StaffRanks.values()) {
staffMembers.put(new Gson().fromJson(staffList.get(rank.getName()), listType), rank);
}
} catch (Exception e) {
e.printStackTrace();
}
}
And this is how I want to use it:
int index = 39550;
for (String player : allStaffNames) {
onlineStatus = "#red#Offline";
if (onlineStaff.contains(PlayerHandler.getPlayer(player))) {
onlineStatus = "#gre#Online";
}
if (staffMembers.containsKey(player)) {
playerClient.getPA().sendFrame126("[" + staffMembers.get(player).getName() + "]#cya#" + Misc.formatPlayerName(player) + ": ", index);
}
playerClient.getPA().sendFrame126(onlineStatus, index - 1);
index += 2;
}
Now, the question is: how I can get the name from the enum inside the Multimap?
Here is what I tried. But, it didn't work.
staffMembers.get(player).getName()
Here is what the Staff_list.json file looks like:
{
"owners":['chuck'],
"developers":['tyrant','stan'],
"serverManagers":['logical'],
"staffManagers":['bench press', 'bench press2'],
"communityManagers":['cleave'],
"administrators":['zeven', 'reavers', 'redaeran', 'stuart'],
"moderators":['searwen', 'potentials', 'maud', 'lickylicky'],
"helpers":['megafun12', 'rhysj', 'vaping', 'punch']
}
Because staffMembers is defined as Multimap<String, StaffRanks>, it means that you should read this as Map<String, Collection<StaffRanks>> with shiny Guava interface for such case (i.e. mapping key to a collection of values). So in your case what you're actually trying to do is:
Collection<StaffRanks> staffRanksForPlayer = staffMembers.get(player);
staffRanksForPlayer.getName(); // BOOM!
If you read / attached an error message, it would be much easier. Anyway, you have two options here:
You're using wrong collection type because you want Map<String, StaffRank> if one player can have only one rank - if so, change collection to Map and you're good.
You have to find a matching rank somehow or use all of them in your code. For example, the latter could be achieved with Java 8:
List<String> staffRanksNamesForPlayer = staffMembers.get(player).stream()
.map(StaffRanks::getName)
.collect(Collectors.toList());
// use list's `toString()` in your code to print its contents
playerClient.getPA().sendFrame126("[" + staffRanksNamesForPlayer.toString()
+ "]#cya#" + Misc.formatPlayerName(player) + ": ", index);
One more thing: generally you want to specify which specific Multimap interface you're using: ListMultimap or SetMultimap.
My goal is to read a JSON file and understand the location of all the values, so that when I encounter that same JSON, I can easily read all the values. I am looking for a way to return a list containing all of the paths to each data value, in Jayway JsonPath format.
Example JSON:
{
"shopper": {
"Id": "4973860941232342",
"Context": {
"CollapseOrderItems": false,
"IsTest": false
}
},
"SelfIdentifiersData": {
"SelfIdentifierData": [
{
"SelfIdentifierType": {
"SelfIdentifierType": "111"
}
},
{
"SelfIdentifierType": {
"SelfIdentifierType": "2222"
}
}
]
}
}
Ideally I would like to take that JSON as a String and do something like this:
String json = "{'shopper': {'Id': '4973860941232342', 'Context': {'CollapseOrderItems': false, 'IsTest': false } }, 'SelfIdentifiersData': {'SelfIdentifierData': [{'SelfIdentifierType': {'SelfIdentifierType': '111'} }, {'SelfIdentifierType': {'SelfIdentifierType': '2222'} } ] } }";
Configuration conf = Configuration.defaultConfiguration();
List<String> jsonPaths = JsonPath.using(conf).parse(json).read("$");
for (String path : jsonPaths) {
System.out.println(path);
}
This code would print this, which is the location of all values in the JSON:
$.shopper.Id
$.shopper.Context.CollapseOrderItems
$.shopper.Context.IsTest
$.SelfIdentifiersData[0].SelfIdentifierData.SelfIdentifierType.SelfIdentifierType
$.SelfIdentifiersData[1].SelfIdentifierData.SelfIdentifierType.SelfIdentifierType
Then ideally, I would be able to take that list and parse the same JSON object to get each value present.
//after list is created
Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
for (String path : jsonPaths) {
Object value = JsonPath.read(document, path);
//do something
}
I am aware that I could get a Map that is a representation of the JSON file, but I am not sure that provides the same ease of access to retrieve all the values. If there is a easy way to do with JSONPath, that would be great, otherwise any other approaches are welcome.
I came up with a solution, sharing in case anyone else is looking for the same thing:
public class JsonParser {
private List<String> pathList;
private String json;
public JsonParser(String json) {
this.json = json;
this.pathList = new ArrayList<String>();
setJsonPaths(json);
}
public List<String> getPathList() {
return this.pathList;
}
private void setJsonPaths(String json) {
this.pathList = new ArrayList<String>();
JSONObject object = new JSONObject(json);
String jsonPath = "$";
if(json != JSONObject.NULL) {
readObject(object, jsonPath);
}
}
private void readObject(JSONObject object, String jsonPath) {
Iterator<String> keysItr = object.keys();
String parentPath = jsonPath;
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
jsonPath = parentPath + "." + key;
if(value instanceof JSONArray) {
readArray((JSONArray) value, jsonPath);
}
else if(value instanceof JSONObject) {
readObject((JSONObject) value, jsonPath);
} else { // is a value
this.pathList.add(jsonPath);
}
}
}
private void readArray(JSONArray array, String jsonPath) {
String parentPath = jsonPath;
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
jsonPath = parentPath + "[" + i + "]";
if(value instanceof JSONArray) {
readArray((JSONArray) value, jsonPath);
} else if(value instanceof JSONObject) {
readObject((JSONObject) value, jsonPath);
} else { // is a value
this.pathList.add(jsonPath);
}
}
}
}
Refer to this utility : https://github.com/wnameless/json-flattener
Perfect answer to your requirement. Provides Flattened map and Flattened strings for complex json strings.
I am not the author of this but have used it successfully for my usecase.
I have an object with unknown values, say
{
"data": [
{"a":...,
"dont_know_what_else_is_here":....}
]
}
And I just want to store the value of "data" as a string into a variable/database.
How should I read it from the streaming API?
Provided that you have already read this object into JsonNode, you can do it like this:
String content = jsonNode.get("data").textValue();
UPD: since you're using a streaming parser, this example on Jackson usage might help.
UPD: the method name is now textValue() - docs
When we try to fetch data in form of string from JsonNode,we usually use asText, but we should use textValue instead.
asText: Method that will return a valid String representation of the container value, if the node is a value node (method isValueNode() returns true), otherwise empty String.
textValue: Method to use for accessing String values. Does NOT do any conversions for non-String value nodes; for non-String values (ones for which isTextual() returns false) null will be returned. For String values, null is never returned (but empty Strings may be)
So Let's take an example,
JsonNode getJsonData(){
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("anyParameter",null);
return node;
}
JsonNode node = getJsonData();
json.get("anyParameter").asText() // this will give output as "null"
json.get("").textValue() // this will give output as null
You can get the data in a map according to key value pairs.
Map<String, Object> mp = mapper.readValue(new File("xyz.txt"),new TypeReference<Map<String, Object>>() {});
Now get the value from map:
mp.get("data");
Assuming you've got a parser already and it points to "data" token (e.g. from custom deserializer) you can do the following:
ObjectMapper mapper = new ObjectMapper();
JsonNode treeNode = mapper.readTree(parser);
return treeNode.toString();
This will get you String containing the value of the "data".
You can have some Entity class for JSON result.
String json = "your_json";
ObjectMapper mapper = new ObjectMapper();
Entity entity = mapper .readValue(json, Entity.class);
// here you can do everything with entity as you wish
// to write Entity value as String when you wish
String text = mapper.writeValueAsString(object);
// to write Entity child's value as String when you wish (let's data contain data part)
String data = mapper.writeValueAsString(object.getData());
Let's say you have a POJO java class called User(Taken from this)
public class User {
public enum Gender { MALE, FEMALE };
public class Name {
private String _first, _last;
public String getFirst() { return _first; }
public String getLast() { return _last; }
public void setFirst(String s) { _first = s; }
public void setLast(String s) { _last = s; }
}
private Gender _gender;
private Name _name;
private boolean _isVerified;
private byte[] _userImage;
public Name getName() { return _name; }
public boolean isVerified() { return _isVerified; }
public Gender getGender() { return _gender; }
public byte[] getUserImage() { return _userImage; }
public void setName(Name n) { _name = n; }
public void setVerified(boolean b) { _isVerified = b; }
public void setGender(Gender g) { _gender = g; }
public void setUserImage(byte[] b) { _userImage = b; }
}
So now you have JSON string which is coming from any place somewhere like web socket or somewhere else.As an example assume this is the string you are getting
String json = "{\n" +
" \"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" },\n" +
" \"gender\" : \"MALE\",\n" +
" \"verified\" : false,\n" +
" \"userImage\" : \"Rm9vYmFyIQ==\"\n" +
"}";
Now you can convert this JSON string into POJO object just using this piece of code.
ObjectMapper mapper = new ObjectMapper();
User user = mapper .readValue(json, User.class);
I assume you just want to read a sub-tree from input using Streaming API -- but in the end, you need to whole sub-tree as one thing to store in DB (or variable).
So what you probably want to use is JsonParser.readValueAs(MyType.class) -- this will call ObjectMapper (and for it to work, parser has to be created via JsonFactory accessed from ObjectMapper; or you need to call JsonFactory.setCodec(mapper)).
If content is arbitrary, you can read it as Map or JsonNode:
Map<String,Object> map = parser.readValueAs(Map.class);
// or
JsonNode root = parser.readValueAsTree();
as soon as JsonParser is pointing to START_ELEMENT of the JSON Object you want to databind.
If using String data in createParser(data), i use this method to collect string content with streaming api:
if ("data".equals(fieldname))
String strData = getValueAsString(jp);
and
private static String getValueAsString(JsonParser jp)
throws com.fasterxml.jackson.core.JsonParseException, IOException {
JsonToken token = jp.getCurrentToken();
int counter = 0;
long startIndex = jp.getCurrentLocation().getCharOffset();
long endIndex = 0;
if (token == JsonToken.START_OBJECT) {
// JsonLocation location = jp.getCurrentLocation();
// startIndex = location.getCharOffset();
// System.out.println(",location=" + new Gson().toJson(location) +
// ", start=" + startIndex);
counter++;
while (counter > 0) {
token = jp.nextToken();
if (token == JsonToken.START_OBJECT)
counter++;
if (token == JsonToken.END_OBJECT) {
counter--;
endIndex = jp.getCurrentLocation().getCharOffset();
}
}
return data.substring((int) startIndex - 1, (int) endIndex);
} else if (token == JsonToken.START_ARRAY) {
counter++;
while (counter > 0) {
token = jp.nextToken();
if (token == JsonToken.START_ARRAY)
counter++;
if (token == JsonToken.END_ARRAY) {
counter--;
endIndex = jp.getCurrentLocation().getCharOffset();
}
}
return data.substring((int) startIndex - 1, (int) endIndex);
} else {
return jp.getText();
}
}
it works when data source is String. for none string source such as file, using JsonLocation.getByteOffset() instead of JsonLocation.getCharOffset()
Finally figured out the solution from reading the tutorial page.
Just to give others a pointer here:
Switch to using a MappingJSONFactory when creating the parser:
HttpResponse response = client.execute(request);
JsonFactory jfactory = new MappingJsonFactory();
JsonParser parser=jfactory.createJsonParser(response.getEntity().getContent());
Then you can just do
parser.readValueAsTree().toString();
or parse it however you want.
I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL classes. I.E. it would figure out the field names and values and send them out to a String.
Getting the field names is fairly simple, here is what a co-worker came up with:
public static List initFieldArray(String className) throws ClassNotFoundException {
Class c = Class.forName(className);
Field field[] = c.getFields();
List<String> classFields = new ArrayList(field.length);
for (int i = 0; i < field.length; i++) {
String cf = field[i].toString();
classFields.add(cf.substring(cf.lastIndexOf(".") + 1));
}
return classFields;
}
Using a factory I could reduce the performance overhead by storing the fields once, the first time the toString() is called. However finding the values could be a lot more expensive.
Due to the performance of reflection this may be more hypothetical then practical. But I am interested in the idea of reflection and how I can use it to improve my everyday programming.
Apache commons-lang ReflectionToStringBuilder does this for you.
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
// your code goes here
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
Another option, if you are ok with JSON, is Google's GSON library.
public String toString() {
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}
It's going to do the reflection for you. This produces a nice, easy to read JSON file. Easy-to-read being relative, non tech folks might find the JSON intimidating.
You could make the GSONBuilder a member variable too, if you don't want to new it up every time.
If you have data that can't be printed (like a stream) or data you just don't want to print, you can just add #Expose tags to the attributes you want to print and then use the following line.
new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create()
.toJson(this);
W/reflection, as I hadn't been aware of the apache library:
(be aware that if you do this you'll probably need to deal with subobjects and make sure they print properly - in particular, arrays won't show you anything useful)
#Override
public String toString()
{
StringBuilder b = new StringBuilder("[");
for (Field f : getClass().getFields())
{
if (!isStaticField(f))
{
try
{
b.append(f.getName() + "=" + f.get(this) + " ");
} catch (IllegalAccessException e)
{
// pass, don't print
}
}
}
b.append(']');
return b.toString();
}
private boolean isStaticField(Field f)
{
return Modifier.isStatic(f.getModifiers());
}
If you're using Eclipse, you may also have a look at JUtils toString generator, which does it statically (generating the method in your source code).
You can use already implemented libraries, as ReflectionToStringBuilder from Apache commons-lang. As was mentioned.
Or write smt similar by yourself with reflection API.
Here is some example:
class UniversalAnalyzer {
private ArrayList<Object> visited = new ArrayList<Object>();
/**
* Converts an object to a string representation that lists all fields.
* #param obj an object
* #return a string with the object's class name and all field names and
* values
*/
public String toString(Object obj) {
if (obj == null) return "null";
if (visited.contains(obj)) return "...";
visited.add(obj);
Class cl = obj.getClass();
if (cl == String.class) return (String) obj;
if (cl.isArray()) {
String r = cl.getComponentType() + "[]{";
for (int i = 0; i < Array.getLength(obj); i++) {
if (i > 0) r += ",";
Object val = Array.get(obj, i);
if (cl.getComponentType().isPrimitive()) r += val;
else r += toString(val);
}
return r + "}";
}
String r = cl.getName();
// inspect the fields of this class and all superclasses
do {
r += "[";
Field[] fields = cl.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
// get the names and values of all fields
for (Field f : fields) {
if (!Modifier.isStatic(f.getModifiers())) {
if (!r.endsWith("[")) r += ",";
r += f.getName() + "=";
try {
Class t = f.getType();
Object val = f.get(obj);
if (t.isPrimitive()) r += val;
else r += toString(val);
} catch (Exception e) {
e.printStackTrace();
}
}
}
r += "]";
cl = cl.getSuperclass();
} while (cl != null);
return r;
}
}
Not reflection, but I had a look at generating the toString method (along with equals/hashCode) as a post-compilation step using bytecode manipulation. Results were mixed.
Here is the Netbeans equivalent to Olivier's answer; smart-codegen plugin for Netbeans.