Is there a JSON format for editing JSON? - 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.

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 this valid JSON for parsing?

I've got a task to write a JSON parser in java with a little help.
I'm already able to parse this:
{
"ArrayWithOneString" : [ "ArrayContent" ],
"Array" : [
{
"ArrayinArray" : [
{
"NumberInArray" : 1337,
"StringInArray" : "String"
}
]
}
]
}
I've got only one last problem:
"string" : { // The bracket
"string" : "valueString"
},
My problem is that I expect a value and not another object for this opening bracket ({).
I wanted to ask if this is valid json before trying to parse it.
Yes it is valid. Well without your highlighting attempts, and assuming it is part of a parent object.
Just because you have a property called "string" doesn't mean it has to be a string value. I suggest perhaps whoever made it just isn't being very consistent, but it is still valid.
The question is, why are you expecting a value? Either the person who constructed the JSON has not done it to specification, or it is you that is not understanding the specification.
Also, you can easily validate JSON here.
Look here for specifications. Your example is valid according to this.
Yes it is valid JSON. You can now parse in your code.
You can check Valid JSON
See below screenshot.

Controlling JSON output format

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!

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)