Java Properties file to JSON using Jackson - java

I would like to convert a Java Properties file into JSON with nested objects. I followed an example I found here but I can't get it to work.
For example, given:
objectA.version=1.0
objectA.hostname=192.168.0.11
objectA.port=9989
objectB.hostname=10.0.2.15
objectB.port=9998
I want:
{
“objectA” : {
"version” : "1.0"
"host” : “192.168.0.11”,
"port" : 9989
},
“objectB” : {
"host” : “10.0.2.15”,
"port" : 9998
}
}
This is what I have so far:
static class Endpoint
{
#JsonProperty("objectA")
public ObjectA objectA;
#JsonProperty("objectB")
public ObjectB objectB;
}
static class ObjectA
{
public String hostname;
public String port;
public String version;
}
static class ObjectB
{
public String hostname;
public String port;
}
try (InputStream input = getClass().getClassLoader().getResourceAsStream("file.properties"))
{
JavaPropsMapper mapper = new JavaPropsMapper();
Endpoint host = mapper.readValue(input, Endpoint.class);
String asText = mapper.writeValueAsString(host);
System.out.println(asText);
}
The output looks like this:
objectA.version=1.0
objectA.hostname=192.168.0.11
objectA.port=9989
objectB.hostname=10.0.2.15
objectB.port=9998

I figured it out.
try (InputStream input = getClass().getClassLoader().getResourceAsStream("file.properties"))
{
JavaPropsMapper mapper = new JavaPropsMapper();
Endpoint host = mapper.readValue(input, Endpoint.class);
// String asText = mapper.writeValueAsString(host);
// add this
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String asText = ow.writeValueAsString(host);
//
System.out.println(asText);
}

Not sure if the OP needs an interim object OR if he just wants to get from a props file to a JSON representation of it. If it is the latter then it's easier to just use the databind ObjectNode class as the interim. E.g.
try (InputStream input = new FileInputStream("path_to.properties")) {
JavaPropsMapper mapper = new JavaPropsMapper();
ObjectNode node = mapper.readValue(input, ObjectNode.class);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
// Alternatively write to file ???
System.out.println(ow.writeValueAsString(node);
} catch (IOException e) {
// Do something
}

Related

How to pass json array under request body using simply json

I am using below code to automate REST API.
Please help me to understand how can I put whole json data for sample data mentioned below as the input has arrays whereas till now I used flat jsons without arrays
Method Dummy()
{
RestAssured.baseURI ="http://mydummyURL";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("id", "THAILAND"); //Issue is with this code
request.header("Content-Type", "application/json");
request.body(requestParams.toJSONString());
Response response = request.post("/EndPoint");
}
where the json body looks like this
{
"tag1": "value1",
"tag2": "value2",
"tag3": {
"tag31": "value31",
"tag32": "value32"
},
"tag4": [{
"domainName": "ABC",
"domainId": "123ABC123",
"domainGUID": "TestMyDomain"
},
{
"domainName": "XYZ",
"domainId": "123XYZ123",
"domainGUID": "TestMyDomain"
}
]
}
ArrayList<JSONObject> array= new ArrayList<JSONObject>();
JSONObject json= new JSONObject();
try {
json.put("key", "value");// your json
} catch (JSONException e) {
e.printStackTrace();
}
array.add(json);
String printjsonarray= array.toString();// pass this into the request
ObjectMapper mapper = new ObjectMapper();
//Create a Java Class for the variables inside array.
JsonArrayData tag4paramVal1 = new JsonArrayData("ABC","123ABC123","TestMyDomain");
JsonArrayData tag4paramVal2 = new JsonArrayData("XYZ","123XYZ123","TestMyDomain");
Object[] tag4ValArray = {tag4paramVal1,tag4paramVal2};
String reqJson = null;
List<String> tag4Data = new ArrayList<String>();
for(Object obj:tag4ValArray){
reqJson = mapper.writeValueAsString(obj);
System.out.println(reqJson);
tag4Data.add(reqJson);
}
System.out.println(tag4Data);
HashMap<String,List<String>> finalReq = new HashMap<String,List<String>>();
finalReq.put("\"tag4\":",tag4Data);
String finalreqString = finalReq.toString();
System.out.println(finalreqString);
finalreqString = finalreqString.replace('=', ' ');
System.out.println(finalreqString);
//Use the above String as a parameter to POST request. You will get your desired JSON array .
//JsonArrayData class code
public class JsonArrayData {
String domainName;
String domainId;
String domainGUID;
public JsonArrayData(String domainName,String domainId,String domainGUID){
this.domainName = domainName;
this.domainId = domainId;
this.domainGUID = domainGUID;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public String getDomainId() {
return domainId;
}
public void setDomainId(String domainId) {
this.domainId = domainId;
}
public String getDomainGUID() {
return domainGUID;
}
public void setDomainGUID(String domainGUID) {
this.domainGUID = domainGUID;
}
}

Java - Convert JSON to XML with values on root tag

I need to convert JSON to XML format with values in the root tag
I have and Orden class configured with #JsonRootName("orden") and added #JsonProperty to all properties.
Actually, I have an inplementation that converts JSON to XML but wich "values in child nodes".
This is my converter implementation:
public static String convertJsonObjectToXml (Object obj) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String jsonString = mapper.writeValueAsString(obj);
JSONObject json = new JSONObject(jsonString);
String xml = XML.toString(json);
return xml;
}
The result of that implementation is this:
<orden>
<formaOp>C</formaOp>
<agente>109</agente>
<tipo>C</tipo>
<precio>2.5</precio>
<tipoVenc>72</tipoVenc>
<idOrigen>156934</idOrigen>
<instrumento>TS</instrumento>
<ejecucion>SINCRONICA</ejecucion>
<agenteCtpte>3</agenteCtpte>
<comitente>0</comitente>
<fechaOrigen>2013-10-09T08:04:13</fechaOrigen>
<cantidad>10</cantidad>
</orden>
But I need the shorter, unfriendly or whatever format's name of the XML below
<orden idOrigen="156934" fechaOrigen="2014-02-19T15:11:44.000-03:00"
agente="109" agenteCtpte="3" tipo="C" ejecucion="SINCRONICA" instrumento="TS"
cantidad="10" precio="2.5" formaOp="C" tipoVenc="72"/>
Any Ideas? Thanks!
Based on the comment posted by Andreas, I used JAXB and my Orden class now is hybrid (can be converted from JSON or XML).
This is how the object is configured:
#XmlRootElement
public class IngresarOrden extends DatosOferta {
private String accion;
private ModoEjecucion ejecucion;
private String operador;
#XmlAttribute
public String getAccion() {
return accion;
}
public void setAccion(String accion) {
this.accion = accion;
}
#XmlAttribute
public ModoEjecucion getEjecucion() {
return ejecucion;
}
public void setEjecucion(ModoEjecucion ejecucion) {
this.ejecucion = ejecucion;
}
#XmlAttribute
public String getOperador() {
return operador;
}
public void setOperador(String operador) {
this.operador = operador;
}
The #XmlAttribute is the annotation needed to put the values in the root tag.
This is the full converter implementation:
public static <T> String converToXml (T obj) throws CommonsException {
JAXBContext jaxbContext;
String xml = null;
try {
OutputStream os = new ByteArrayOutputStream();
jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(obj, os);
xml = os.toString();
} catch (JAXBException e) {
logger.error("XML converter exception: ", e);
throw new CommonsException("XML converter exception: ", e);
}
return xml;
}
Thanks!

Json parsing with nested array using Gson

I have not seen an (answered) example on the web which discusses this kind of nested-json-array.
JSON to be parsed:
{
"Field": {
"ObjectsList": [
{
"type": "Num",
"priority": "Low",
"size": 3.43
},
{
"type": "Str",
"priority": "Med",
"size": 2.61
}
]
}
}
I created a class for each 'level' of nested json block. I want to be able to parse the contents of the "ObjectList" array.
Can anyone help me to parse this JSON using Gson in Java?
Any hints or code-snippets would be greatly appreciated.
My approach is the following:
public static void main (String... args) throws Exception
{
URL jsonUrl = new URL("http://jsonUrl.com") // cannot share the url
try (InputStream input = jsonUrl.openStream();
BufferedReader buffReader = new BufferedReader (new InputStreamReader (input, "UTF-8")))
{
Gson gson = new GsonBuilder().create();
ClassA classA = gson.fromJson(buffReader, ClassA.class);
System.out.println(classA);
}
}
}
class ClassA
{
private String field;
// getter & setter //
}
class ClassB
{
private List<ClassC> objList;
// getter & setter //
}
clas ClassC
{
private String type;
private String priority;
private double size;
// getters & setters //
public String printStr()
{
return String.format(type, priority, size);
}
}
The following snippet and source file would help you:
https://github.com/matpalm/common-crawl-quick-hacks/blob/master/links_in_metadata/src/com/matpalm/MetaDataToTldLinks.java#L17
private static ParseResult NO_LINKS = new ParseResult(new HashSet<String>(), 0);
private JsonParser parser;
public static void main(String[] s) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(s[0]));
MetaDataToTldLinks metaDataToTldLinks = new MetaDataToTldLinks();
while (reader.ready()) {
String[] fields = reader.readLine().split("\t");
ParseResult outboundLinks = metaDataToTldLinks.outboundLinks(fields[1]);
System.out.println(tldOf(fields[0]) + " " + outboundLinks.links);
}
}
public MetaDataToTldLinks() {
this.parser = new JsonParser();
}
public ParseResult outboundLinks(String jsonMetaData) {
JsonObject metaData = parser.parse(jsonMetaData.toString()).getAsJsonObject();
if (!"SUCCESS".equals(metaData.get("disposition").getAsString()))
return NO_LINKS;
JsonElement content = metaData.get("content");
if (content == null)
return NO_LINKS;
JsonArray links = content.getAsJsonObject().getAsJsonArray("links");
if (links == null)
return NO_LINKS;
Set<String> outboundLinks = new HashSet<String>();
int numNull = 0;
for (JsonElement linke : links) {
JsonObject link = linke.getAsJsonObject();
if ("a".equals(link.get("type").getAsString())) { // anchor
String tld = tldOf(link.get("href").getAsString());
if (tld == null)
++numNull;
else
outboundLinks.add(tld);
}
}
return new ParseResult(outboundLinks, numNull);
}
public static String tldOf(String url) {
try {
String tld = new URI(url).getHost();
if (tld==null)
return null;
if (tld.startsWith("www."))
tld = tld.substring(4);
tld = tld.trim();
return tld.length()==0 ? null : tld;
}
catch (URISyntaxException e) {
return null;
}
}
public static class ParseResult {
public final Set<String> links;
public final int numNull;
public ParseResult(Set<String> links, int numNull) {
this.links = links;
this.numNull = numNull;
}
}
How about this snippet?:
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
List<Object> out = Lists.newArrayListWithCapacity(array.size());
for (JsonElement item : array) {
out.add(toRawTypes(item));
}
}

xStream changes order of objects in write/readObject with JSON serialization

We use XStream to serialize objects to JSON and vice versa.
We init xStream like this
XStream xStream = new XStream(new JettisonMappedXmlDriver(new Configuration(), false));
xStream.ignoreUnknownElements();
xStream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
We have test class
public static class TestWOWithBI implements Serializable{
private static final long serialVersionUID = -4720678317857471031L;
private transient String customerNickname;
private transient String customerUuid;
private transient BigInteger discussionId;
private transient String message;
public TestWOWithBI(String customerNickname, String customerUuid, BigInteger discussionId, String message){
this.customerNickname = customerNickname;
this.customerUuid = customerUuid;
this.discussionId = discussionId;
this.message = message;
}
private final void writeObject(final ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(customerNickname);
out.writeObject(customerUuid);
out.writeObject(discussionId);
out.writeObject(message);
}
private final void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException{
in.defaultReadObject();
customerNickname = (String) in.readObject();
customerUuid = (String) in.readObject();
discussionId = (BigInteger) in.readObject();
message = (String) in.readObject();
}
}
After serialization it looks like this:
{
"somethere.ObjectToJSONSerializerTest$TestWOWithBI": {
"#serialization": "custom",
"somethere.ObjectToJSONSerializerTest$TestWOWithBI": {
"default": "",
"string": ["name",
"uuid",
"message"],
"big-int": 1
}
}
}
and deserialization fails with class cast. It was on 1.3.1 and 1.4.7 versions. Looks like bug to me, but may be where is some settings?
UPD:
Seems like org.codehaus.jettison.mapped.MappedXMLStreamWriter.JSONPropertyObject#withProperty
if(old != null) {
JSONArray values;
// Convert an existing property to an array
// and append to the array
if (old instanceof JSONArray) {
values = (JSONArray)old;
} else {
values = new JSONArray();
values.put(old);
}
values.put(value);
object.put(property.getKey(), values);
} else if(getSerializedAsArrays().contains(property.getKey())) {
JSONArray values = new JSONArray();
values.put(value);
object.put(property.getKey(), values);
} else {
// Add the property directly.
object.put(property.getKey(), value);
}
It just group elements of same type.

Converting json from a file to a java object

I am trying to convert json from a text file into a java object.
I have tried both the jackson library, I put in the dependency and what not. My json file has both camel case and underscores, and that is causing an error when running my program. Here is the code that I used for when relating to the gson librar and it does not do anything, the output is the same with or without the code that I placed.
java.net.URL url = this.getClass().getResource("/test.json");
File jsonFile = new File(url.getFile());
System.out.println("Full path of file: " + jsonFile);
try
{
BufferedReader br = new BufferedReader(new FileReader("/test.json"));
// convert the json string back to object
DataObject obj = gson.fromJson(br, DataObject.class);
System.out.println(obj);
} catch (IOException e)
{
e.printStackTrace();
}
Now I also tried the jackson library. Here is the code i used
java.net.URL url = this.getClass().getResource("/test.json");
File jsonFile = new File(url.getFile());
System.out.println("Full path of file: " + jsonFile);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
InputStream is = Test_Project.class.getResourceAsStream("/test.json");
SampleDto testObj = mapper.readValue(is, SampleDto.class);
System.out.println(testObj.getCreatedByUrl());
I am not sure what to do,
This simple example works like a charm:
DTOs
public class SampleDTO
{
private String name;
private InnerDTO inner;
// getters/setters
}
public class InnerDTO
{
private int number;
private String str;
// getters/setters
}
Gson
BufferedReader br = new BufferedReader(new FileReader("/tmp/test.json"));
SampleDTO sample = new Gson().fromJson(br, SampleDTO.class);
Jackson
InputStream inJson = SampleDTO.class.getResourceAsStream("/test.json");
SampleDTO sample = new ObjectMapper().readValue(inJson, SampleDTO.class);
JSON (test.json)
{
"name" : "Mike",
"inner": {
"number" : 5,
"str" : "Simple!"
}
}
public static void main(String args[]){
ObjectMapper mapper = new ObjectMapper();
/**
* Read object from file
*/
Person person = mapper.readValue(new File("/home/document/person.json"), Person.class);
System.out.println(person);
}
A common way of getting both array of json in file or simply json would be
InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, Employee.class);
List<Employee> lstEmployees = mapper.readValue(inputStream, collectionType);
The file.json needs to be placed in the resources folder. If your file only has a json block without json array square brackets [] , you can skip the CollectionType
InputStream inputStream= Employee.class.getResourceAsStream("/file.json");
Employee employee = mapper.readValue(inputStream, Employee.class);
Also refer here for original question from where I have drawn.

Categories