kafka-protobuf-console-consumer and preservingProtoFieldNames - json

I'm using kafka-protobuf-console-consumer which converts binary protobuf messages to json. It works fine, but it is not using the original protobuf message field names. It converts snake_case to snakeCase. I would like to use snake_case.
Is it possible to chagne its behaviour, just the way I can call JsonFormat.printer().preservingProtoFieldNames() when formatting protobuf in java? Instead of my messages looking like this: {"helloWorld": "value"}, I want {"hello_world": "value"}

Related

Prevent parsing a JSON node with common-lisp YASON library

I am using the Yason library in common-lisp, I want to parse a json string but would like the parser to keep one a its node unparsed.
Typically with an example like that:
{
"metadata1" : "mydata1",
"metadata2" : "mydata2",
"payload" : {...my long payload object},
"otherNodesToParse" : {...}
}
How can I set the yason parser to parse my json but skip the payload node and keep it as a string in the json format.
Use: let's say I just want the envelope data (everything that's not the payload), and to forward the payload as-is (as json string) to another system.
If I parse the whole json (so including payload) and then re-encode the payload to json, it is inefficient. The payload size could also be pretty big.
How do you know where the end of the payload object is in the stream? You do so by parsing the stream: if you don't parse the stream you simply can't know where the end of the object is: that's the nature of JSON's syntax (as it is the nature of CL's default syntax). For instance the only way you can know the difference between where to continue after
{x:1}
and after
{x:1.2}
is by parsing the two things.
So you must necessarily parse the whole thing.
So the answer to your question is: you can't do this.
You could (but not, I think, with YASON) decide that you did not want to build an object as a result of the parse. And perhaps, if the stream you are parsing corresponds to something with random access like a string or a file, you could note the start and end positions in the stream to later extract a string from it corresponding to the unparsed data (or you could perhaps build it up as you go).
It looks as if some or all of this might be possible with CL-JSON, but you'd have to work at it.
Unless the objects you are reading are vast the benefit of this seems questionable-to-none. If you really do want to do something like this efficiently you need a serialisation scheme which tells you how long things are.

How to convert a string into json format in flink side?

I received a one digit as string format, for example, which look like 12.
What I want to do is to convert those strings into a json format and
write them to the text file in my local directory.
However, I didn't get the right solution except for those things that manually change the strings so that it looks like the json format. I think it is tedious and laborious tasks.
The completed json format will be shown as below.
{"temperature": 12}
Is there any libraries that achieve my issue?
Thanks.
Check out Gson. In particular, if you have a Java class with a single "temperature" field, then see this for how to convert to Json.

Parsing Ruby JSON/hash to get single value

I am very new to ruby and could not find any examples of what I wanted to do though I am sure there is many out there.
I have a ruby json object that I need to get a certain value out of the first key pair in it. Below is the ruby/json and I want to take that and get the token value from it.
{"token"=>"<my token>", "dc"=>"test-dc"}
How do I get the token?
Ruby Hash objects and JSON "objects" might seem similar superficially but there's important differences.
Ruby's internal Hash looks like this when expressed with inspect:
{"token"=>"<my token>", "dc"=>"test-dc"}
Whereas JSON, which is a language-neutral data format inspired by JavaScript, looks like:
{"token": "<my token>", "dc": "test-dc"}
These are superficially similar, but the important difference is that if you have the former, not as a string but as a Hash, you can just use it:
hash["token"]
If you have the latter you'll need to parse it with something like JSON.load:
JSON.load('{"token": "<my token>", "dc": "test-dc"}')["token"]
Of course if you have Ruby and want JSON you can always dump it:
JSON.dump({"token"=>"<my token>", "dc"=>"test-dc"})
# => "{\"token\":\"<my token>\",\"dc\":\"test-dc\"}
Note that the arguments to dump are actual Ruby code and not a string like in the previous example. The output is a string, and as such, double-quotes need to be escaped with backslash. If you actually print this you'll see it without the extra escaping and it looks normal, that's just an artifact of how Ruby presents strings using inspect.

Json : getting the unwanted result

I'm using json plugin to get the response in json.
But I m getting the unwanted result:
Here is what I get:
{"data":"[[\"service\",\"webservices\",\"document\"],[\"validation\",\"adapters\",\"server\"]]","records":25,"recordsTotal":75}
originally the data var in my action class is like this:
[["service","webservices","document"],["validation","adapters","server"]]
but json plugin adds the backslash.
The wanted result is that:
{"data":[["service","webservices","document"],["validation","adapters","server"]],"records":25,"recordsTotal":75}
Is there a way to get the later result ?
Thanks
You're representing the data as a PHP string. " is obviously a reserved character in JSON, so your serialization library is dutifully escaping the quote using /.
If you set up the PHP variable so it's an array of arrays, instead of a string representing an array of arrays, then your JSON serialization will work fine.

name json variable and jsonString variable convention?

JSON could mean JSON type or json string.
It starts confuse me when different library use json in two different meanings.
I wonder how other people name those variables differently.
For all practical purposes, "JSON" has exactly one meaning, which is a string representing a JavaScript object following certain specific syntax.
JSON is parsed into a JavaScript object using JSON.parse, and an JavaScript object is converted into a JSON string using JSON.stringify.
The problem is that all too many people have gotten into the bad habit of referring to plain old JavaScript objects as JSON. That is either confused or sloppy or both. {a: 1} is a JS object. '{"a": 1}' is a JSON string.
In the same vein, many people use variable names like json to refer to JavaScript objects derived from JSON. For example:
$.getJSON('foo.php') . then(function(json) { ...
In the above case, the variable name json is ill-advised. The actual payload returned from the server is a JSON string, but internally $.getJSON has already transformed that into a plain old JavaScript object, which is what is being passed to the then handler. Therefore, it would be preferable to use the variable name data, for example.
If a library uses the term "json" to refer to things which are not JSON, but actually are JavaScript objects, it is a mark of poor design, and I'd suggest looking around for a different library.