JSON-to-JSON transformation: possible approaches - java

Sometimes there’s a need to perform a same-format transformation from one structure into another.
Now before you start with the implementation, you first need the theoretical basis. therefore my question to you is what possible approaches for the transformation are possible for JSON to JSON?

If you are talking about a JSON string: convert into a HashMap, do what you have to do then convert back to JSON. You can use a library such as JSON.simple
https://mkyong.com/java/json-simple-example-read-and-write-json/

Related

What benefits does JSON dot-notation give over hierarchical structure?

We are working on a Java REST service which accepts JSON input but the consumers of this service want to send the json in dot-notation format instead of hierarchical structure.
So for example they prefer following input -
{
“person.address.city”: “San Francisco”
}
over:
{
“person”:{
“address”:{
“city”: “San Francisco”
}
}
}
Does the former notation have any benefits over the latter?
I could find below article which talks about this way of data representation:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/simple-dot-notation-access-to-json-data.html#GUID-7249417B-A337-4854-8040-192D5CEFD576
Edit: From Java perspective, the hierarchical structure can be deserialized into typed classes. On the other hand, the dot-notation might have to be parsed as strings to get values.
This is just another OVER format (arrangement). Simplifies reading. It has nothing to do with the JSON format itself.
It could also be:
{
“person/address/city”: “San Francisco”
}
One benefit of the dot notation is that it flattens out the hierarchical structure, making it easier to store / manipulate the data in non hierarchical systems.
Using dot notation, you can transform a complex tree structure to a simple key value pairs structure. You can then store it to a simple Java HashMap or even in a Redis Hash.
This is how traditional key-value stores can expand their use cases to include more complex storage requirements.

How to convert JsonForm to Json?

I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.

How to convert thrift objects to readable string and convert it back?

Sometimes, we need to create some thrift objects in unit tests. We can do it by manually create object using Java code, like:
MyObj myObj = new MyObj();
myObj.setName("???");
myObj.setAge(111);
But which is not convenient. I'm looking for a way to create objects with some readable text.
We can convert thrift objects to JSON with TSimpleJSONProtocol, and get very readable JSON string, like:
{ "name": "???", "age": 111 }
But the problem is TSimpleJSONProtocol is write only, thrift can't read it back to construct an instance of MyObj.
Although there is a TJSONProtocol which supports to serialize and deserialize, but the generated JSON is not readable, it uses a very simplified JSON format and most of the field names are missing. Not convenient to construct it in tests.
Is there any way to convert thrift objects to readable string and also can convert it back? If TSimpleJSONProtocol supports converting back, which is just what I'm looking for
The main goal of Thrift is to provide efficient serialization and RPC mechanisms. What you want is something that is - at least partially - contrary to that. Human-readable data structures and machine processing efficiency are to a good extent conflicting goals, and Thrift favors the latter over the former.
You already found out about the TSimpleJson and TJson protocols and about their pros and cons, so there is not much to add. The only thing that is left to say is this: the protocol/transport stack of Thrift is simple enough.
This simplicity makes it possible to add another protocol based on your specific needs without much or overly complicated work. One could probably even write an XML protocol (if anyone really wants such bloatware) in short time.
The only caveat, especially vis-à-vis your specific case, is the fact that Thrift needs the field ID to deserialize the data. So you either need to store them in the data, or you need some other mechanism which is able to retrieve that field ID based on the field and structure names.

json schema containing multiple independent types

Is there any example to show how a json schema can be written to include multiple independent objects in one file?
If its possible, is there any java library that can convert all of them to individual pojos and vice versa?
JSON requires you to always have either one array or one object as root element. If that is not the case, it is not valid JSON.
That being said, why don't you just wrap your multiple indidpendent objects in one root object? Doing this, you will be able to use any functional JSON marshaller.

JSON, XML or String concatenation

I am doing a new application where I want to choose which protocol to use in it. I tried the String concatenation and the XML before, but never tried the JSON Object. Well Which one of those three is better in terms of performance? I am aware that XML is way much better than string concatenation. So what to use? XML or JSON? Or maybe a new technology that I am not aware of?
Thanks in advance
I am aware that XML is way much better than string concatenation. Well in this I mean that in String concatenation, I am adding different values and splitters to a string and then looping to find the spliters on the device. like in the example:
String toSend = "test1////test2////test3////test4////test5";
Here the splitter is "////" and I am sending 5 values. Getting these 5 values will be much more slower than XML in case of thousands of values.
It depends. :)
Well, actually I think a properly written code to split a string will be more fast than an XML/JSON parser, however XML/JSON parsers are reliable in terms of returning exactly the same data structure. For instance, how would you handle a case when your data itself includes splitters? If such case is impossible under your business logic, then you may just go with string joining/splitting. Otherwise it is better not to reinvent the wheel and just use XML/JSON (JSON is more lightweight).
It depends on the kind of Objects you will be exchanging.
It also depends on the way you will request and use you objects.
If you want ot provide a REST service that exposes simples Objects will be accessible directly by as Javascript GUI. I would also go for JSON. But no hand-made String concatenation to build JSON. You can use a lib.
But I you plan to exchange more complex data, between various Java based "services". I would probably go for XML. Especially if you can first write the XSD that defines you XML objects. You will be able to generate Java class and let JAXB do the marshalling/unmarshalling boring stuff.
I would choose JSON, it's very portable and lightweight (lighter than XML).

Categories