In a POST does order of JSON variables matter? - java

Im writing a program to upload my companies orders to an order review site through a POST request. It takes a JSON Object, and it starts like this
{
"utoken": "XVUYvqaRLPtjfuj1OyNbyqw1cv0R0f76g4PadwmR",
"platform": "general",
However when I create my JSONObject using JSON.simple
JSONObject test = new JSONObject();
test.put("utoken", "awooga");
test.put("platform", "general");
It puts it into alphabetical order when I print it out
{
"platform": "general",
"utoken": "awooga"
Does this matter? I dont think it should, but just want to make certain as I've never ran into this before.

As per JSON standard, official definition of object states:
An object is an unordered set of name/value pairs.
Therefore the order does not matter. Obviously from the perspective of the server receiving the POST request, the order can be parsed from HTTP header and reacted upon. I suppose however this is not of your interest, as it does not make much sense to do so.

See an answer here
TL;DR - order isn't promised to stay the same

Related

Parsing JSON string and preserving key-data order when using json-simple without manually constructing HashMap

I know that this topic has been talked about, and the use of a LinkedHashMap is a 'hacky' way to maneuver this, but if I'm given thousands of JSON strings as input, and eventually want to output them back in their original form, is there anyway to preserve the order without manually constructing LinkedHashMaps.
For example a string like this
{"key":1,"surname":"Reed","given":"Ryan","address":{"state":"CA","postal":"90210"},"gender":"M"}
Right now if I parse the object like so:
JSONObject jsonObject = (JSONObject) parser.parse(str);
System.out.println(jsonObject);
My output will look like this:
{"surname":"Reed","gender":M,"address":{"postalCode":"90210","state":"CA"},"key":1,"given":"Ryan"}
Is there anyway I can get the output to match exactly like the given input?
In Json property structure, order does not matter. but if you have specific order in your mind you can use Jackson to order them in you desirable way, both in your server and client apps.
https://www.baeldung.com/jackson
http://www.davismol.net/2016/10/24/jackson-json-using-jsonpropertyorder-annotation-to-define-properties-serialization-order/
I think it is impossible by default.
You can refer to this RFC https://www.ietf.org/rfc/rfc4627.txt
An array is an ordered sequence of zero or more values.
If you want to hack it you can override the data structure and use the data structure which preserves the order.

Spring/Jackson Mapping Inner JSON Objects

I have a RESTful web service that provides JSON that I am consuming. I am using Spring 3.2 and Spring's MappingJacksonHttpMessageConverter. My JSON looks like this:
{
"Daives": {
"Daive": {},
"Daive": {},
"Daive": {},
"Daive": {}
}
}
Now everything I have read seems to indicate that this JSON should be refactored to an array of JSON Daives. However, this is valid JSON so I want to make sure that I am thinking correctly before going back to the service provider to ask for changes. In the format above, I would have to know ahead of time how many Daives there are going to be such that my DTO accounted for them. The handy dandy Jackson mapper isn't going work with this kind of JSON setup. If the JSON was altered to provide and Array of JSON Daives, I could use a List to dynamically map them using Spring/Jackson.
Am I correct? Thanks :)
According to this thread, the JSON spec itself does not forbid multiple fields with the same name (in your case, multiple fields named "Daive" in the object "Daives").
However, most parsers will either return an error or ignore any value but the last one. As you said, putting these values into an array seems much more sensible; and indeed, you'll be able to map this array to a List with Jackson.

What's the fastest way to build a JSON string in java?

I'm working with JSON on the server for the first time. I'm trying to create an object that looks like this:
{
columnData : {
column1 : {"foo": "bar", "value" : 1 },
},
rowData : {
row1 : {"type" : "integer", "value" : 1},
}
}
Essentially, it's a two-level JSON object in which I've got all objects on the top level. Everything on the second level will be a string, and integer, or a float. I've found several ways I could go about creating this JSON string:
Build the string directly
I can go through one line at a time and build up a giant string. This seems unwieldy.
Use gson
I can build a map (or class?) that has the structure of the JSON I want, and then use gson to convert to a JSON string.
Use JsonObject
This would work like this question. I just found that example, and I like how simple working with JSON looks in it, but I'm not sure if it's a better way to go than the other options.
I'm pretty new with Java, so I'm not really sure what the performance tradeoffs of these methods are. I do know that I'll potentially be calling this method lots of times very quickly to load data into a client as the user scrolls a table in a webapp, so I'd like everything to be as fast as possible. What's the quickest way to go about building the JSON string that I'll then be sending back to the client?
Depends on what "fastest" means. Fast to develop or fast in terms of performance.
Fastest on dev is to use a library to serialize existing data structures directly to JSON. Use the following library.
http://flexjson.sourceforge.net
Performance wise comparisons it matches Jackson and beats in some cases and destroys gson. But that means you would be serializing your data structures directly rather than mapping them by hand with JSONObject or something like that.
You may be able to get a faster transaction / sec by using JSONObject by hand and mapping your data structures to it. You might get better performance, but then you might not because you have to new up JSONObject and convert the data to it. Then allow JSONObject to render. Can you write hand written code better than a serializing library? Maybe, maybe not.

Making POST request containing a list

I've been trying to implement a Scala application which makes POST request to given REST web services. In the request, there is a list of strings. I try to use scalaj but it only allows params as the map from String to String.
So, I wonder if there is anyway to make a POST request containing the list of Strings in Scala, solutions in Java are also welcomed.
After looking and trying around, I finally find the answer which is surprisingly simple. Although scalaj only allows Map from String to String, it also allows list of tuples. By keeping the same first value in the tuples, I can send the list of items.
For example, in order to send the list of numbers whose key is "data", I need to:
Http.post("http://localhost/Dummy").params("data" -> "1", "data" -> "2")

How do I print json structure for a multi-level json using java

I am very new to JSON and am not sure if the term "multi-level" json is correct. If not, please help correct it.
I have been tasked with printing the request and response structure of a given rest service. I have the api.json which refers to a host of json objects, which in turn refer to other json objects and so on...
Please note that I am interested in printing the structure and not the contents of the request and response.
I know that I can go ahead and do a recursive read of the files and get this done. But that does not seem right.
Can someone please provide some pointers for the same?
There is a general misconception about JSON. JSON is a format to represent a complex data structure as a string. That's it. JSON itself doesn't know anything about structure - it's really just a string. What you can do is parse the JSON string to get JavaScript objects (i.e. numbers, strings, arrays and things of type object).
This means that you can't really learn enough about the structure of the rest service by looking at JSON strings it accepts or sends. You need to look at the documentation or the internal objects which the service uses to parse the JSON instead.
Example:
{"foo":"bar"}
That tells you that the REST service accepts a JavaScript object and one of the possible parameters. But it doesn't tell you about the other 50 parameters.
If you only have the JSON, then you can use a parsing library to turn that into something that you can print. But unless you want pretty printing (indent and such), that's the same as printing the JSON string itself, so you don't gain anything.

Categories

Resources