Controlling JSON output format - json

A reverse to the question asked at How to format Json output ...
EclipseLink makes JSON output code from JAXB objects like this:
{
"test" : {
"count" : 10,
"text" : "Craig"
}
}
How do I make it put quotes around the number?
Thanks!

Related

Writing AVRO fixed type from JSON in NIFI

I'm trying to convert a NiFi flow file containing JSON to an AVRO record.
The problem I have is that I don't know how to deal with a fixed type in AVRO, i.e. how to specifiy the proper JSON for converting to fixed?
Currently I'm using the ConvertJsonToAvro-processor.
The AVRO output schema:
{
"type" : "record",
"name" : "Message",
"namespace" : "com.example",
"fields" : [ {
"name" : "MAC",
"type" : {
"type" : "fixed",
"name" : "MY_FIXED_TYPE",
"size" : 6
}
}]
}
The input JSON-forms I tried are
{ "MAC": [ 0, 1, 2, 3, 4, 5] }
{ "MAC": "012345" }
{"MAC":"\u0000\u0001\u0002\u0003\u0004\u0005"}
{"MAC":{"MY_FIXED_TYPE": "\u0000\u0001\u0002\u0003\u0004\u0005"}}
Unfortunately none of them worked for me.
I also tried the ConvertRecord-processor instead of the ConvertJsonToAvro-processor. Also without any luck.
Any ideas?
After some further investigation, it looks like the ConvertJsonToAvro processor can't be used to generate Avro FIXED or BYTES datum. This is likely a bug with NiFi and how the processor uses Avro.
If I'm not mistaken:
The NiFi ConvertJsonToAvro uses the KiteSDK to interpret JSON into Avro data. This JSON-to-Avro conversion is not the same as Avro JSON encoding from the specification.
This processor reads the incoming string into a jackson JsonNode.
FIXED and BYTES types need to correspond to a JsonNode where isBinary() is true.
As far as I can tell, parsing a JSON string with Jackson never generates such a JSON node.
I would raise a NiFi JIRA about this, or an issue on the KiteSDK.
Note: this answer does not apply to NiFi JSON to Avro conversion. My apologies for the mistaken assumption! I'm unsure of the best practice for an answer known to be wrong.
An example of a "correct" Avro JSON encoding for the bytes type is given in the spec. I think you're looking for:
{"MAC":"\u0000\u0001\u0002\u0003\u0004\u0005"}
Or alternatively (for a fixed schema in a union):
{"MAC":{"MY_FIXED_TYPE": "\u0000\u0001\u0002\u0003\u0004\u0005"}}
You can correctly parse this input string using your given Schema.

set object value to another object in a JSON

I just wanted to know if I can do something like this in JSON:
{
"web" : {
"app_pub" : "localhost/public",
"app_lib" : "localhost/lib",
"app_assets" : "app_pub" + "/assets"
}
}
You can check your JSON, using validator, i.e.:
https://jsonformatter.curiousconcept.com/
Your code doesn't correspond RFC4627 and others.
You can't do operations inside JSON.
P.S.:
RFC4627 contains full JSON grammar.

Is there a JSON format for editing JSON?

I'm wondering if there is a JSON format for editing JSON ?
eg if I had some json
{ "first name" : "Joe" }
and + another json file
{ "action" : "add",
"dest" : "root" -- json pointer maybe
"value" : { "surname : "Blogs" }
}
would get me =
{ "first name" : "Joe", "surname : "Blogs" }
similarly a delete , and change ..
Is there a JSON format that does this? It may be part of a noSQL db or may not be or some javascript library - but i'm not after a JS library more is there a JSON format to do this, One would assume someone has done this before!
It doesn't work like that. JSON is just a data-interchange format (http://www.json.org/). There is no transactional semantic or anything that goes with it.

How to read values from nested JSON structure in java?

How to read values from nested JSON without using any library like GSON or org.JSON?
JSON is :
{data: { "EV_TOT_AMT" : "12" , "EV_CURR" : "INR", "T_BASKET" : [{"ORDER" : "abc", "BASE" : "xyx"},{"ORDER" : "def", "BASE" : "mno"}] } }
I want to read specific values as EV_TOT_AMT , EV_CURR , ORDER.
As far as I know, Java doesn't includes a JSON parser inside it's core classes... so if you don't want to use an external library, you'll need to build your own JSON parser.
Of course, you can just search the JSON string for your desired substrings and get the values moving into the string from the next ":" to the next "," (if the first character after the ":" is not an "["). But this isn't a good approach, unless your JSON input string is going to have always the same structure... well... actually that's not a good approach, period.

Flat csv data to Json

I am trying to process a Json data in Java. I have the data in below format (it is nested data structure with arrays etc.)
person.name,person.friend[0],person.friend[1],person.address.city,person.address.country
1,x,y,kolkata,india
2,a,b,london,uk
The first line is header denoting the nested object hierarchy. I want a json in below format,
{
"data" : [
{
"name" : "1",
"friend" : ["x","y"],
"address" : { "city" : "kolkata", "country" : "india" }
},
{
"name" : "2",
"friend" : ["a","b"],
"address" : { "city" : "london", "country" : "uk" }
} ]
}
The object structure is dynamic and I dont know the columns or header in advance, i.e. I can not use any predefined POJO to get populated with the data. In this example, it "Person" object but it may be any object structure.
I have gone through Jackson or Gson API, but none seems to fulfill this requirement. Is there any API that can help? or any other wayout?
Thanks
You need to do it in 2 steps.
First, you have to parse your CSV. I recommend superCSV. Parsing CSV may be fancy sometimes, so I really recommend you to use a library for that.
Second, you can serialize into JSON. Then you can use GSON, jackson, flexjson, whatever.
After a long Google...I found that the only option is to represent a collection based object structure in flat file is repeated rows,
person.name,person.friends,person.address.city,person.address.country
1,x,kolkata,india
1,y,kolkata,india
2,a,london,uk
2,b,london,uk
where the non-array elements repeats. We need to form a json from this, then need to filter or club the same object by its ID (here person.name)