The request generates the "key1", "key2" in Swagger - java

I have the sample request as given below. May I know how to make the request generates the key1, key2, etc. in JSON Swagger? As it seems like not a predefined object from the swagger file.
Sample request:
{
"products": {
"key1": {
"price": 99.146564,
"currencyPair": "USD-MYR",
"decimalPlace": 2
},
"key2": {
"price": 86.1571,
"currencyPair": "USD-IDR"
},
"key3": {
"price": 722.45654,
"currencyPair": "USD-ZNH"
}
}
}
Hope someone can guide me on how to solve it. Thanks.

Related

#JsonIgnoreProperties not working for swagger generation

Am trying to hide few fields of an object for specific API Endpoints.
ex:
"student": {
"name": "value1",
"age": "value2",
"dob": "value3",
"value": "value4"
}
I need these some values to be present and hidden for certain API Endpoints.
API 1: GET : /school/student/personal
"student": {
"name": "value1",
"age": "value2"
}
API 2: GET : /school/student/all
"student": {
"name": "value1",
"age": "value2",
"fatherName": "value3",
"motherName": "value4",
}
For me you are using different object, you could create DTO(Data Transfer Object) and make your API return DTO.
You need adapter to transform your domine object(ex.Student) to DTO object(StudentDTO or TinyStudentDTO)
EX.
API 1: GET : /school/student/personal should return TinyStudentDTO
TinyStudentDTO
-name
-age
API 2: GET : /school/student/all should return StudentDTO
Your StudentDTO will extend with TinyStudent
StudentDTO
-fatherName
-motherName

How to make a JSON Parser to parse elasticsearch mapping in java?

I wanted to parse this structure which is an elasticsearch filter:
{
"filter": {
"name_synonyms_filter": {
"synonym_path": "sample.txt",
"type": "abc_synonym_filter"
},
"name_formatter": {
"name": "name_formatter",
"type": "abc_token_filter"
}
}
}
My question is how can I access individual filters without using key ("name_synonyms_filter" , etc) in java?
your JSON was impropertly formatted.
Here it is fixed:
{
"abc": [{
"name": "somename"
},
{
"name": "somename"
}
]
}
How to parse it:
let x = JSON.parse({
"abc": [{
"name": "somename"
},
{
"name": "somename"
}
]
});
console.log(x);
Let me know if you have any questions.

Creating JSON RPC requests in Java with parameters which have parameters

The title is a bit messy, but what I'm trying to do is create a JSON RPC request which looks like this:
{
"method":"site/method",
"id":1,
"filter":{
"name":"person"
}
}
I'm having trouble finding a way to do that. I'm using the JSONRPCBase library right now but I'm not sure it's compatible with that. Anybody have any suggestions?
Your request structure is not JSON-RPC compliant.
For JSON-RPC 2.0, try:
{
"jsonrpc":"2.0"
"method": "site/method",
"id": 1,
"params": {
"filter": {
"name": "person"
}
}
}
For JSON-RPC 1.0, the parameters must to be an array, depending on your method arguments, so it can vary depending on your implementation. For example:
{
"method": "site/method",
"id": 1,
"params": [{
"filter": {
"name": "person"
}}]
}
}
or
{
"method": "site/method",
"id": 1,
"params": [{
"name": "person"
}]
}
}

Having trouble deserializing JSON response with gson

I am using an API where I supply an input string, and it returns some keyword autocompletions and product nodes.
My goal is to deserialize the response and get a list of the autocompletion Strings I can use. I'm trying implement this in an android application with the Retrofit library, which uses gson.
First off, I'm not sure the response I have is a typical JSON response. The 'nodes' item has key / value pairs, but the input string and the autocompletions list don't seem to have keys I can use.
["pol",
["polaroid camera",
"polo",
"polo ralph lauren",
"polo ralph lauren men",
"polar heart rate monitor",
"polaroid",
"polo shirt",
"polar watch",
"police scanner",
"polar"],
[{
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
}],
[]]
This is my attempt at the java classes for gson to deserialize to. However, it doesn't work as from what I understand, gson needs the class variables to match the JSON keys (true for Node class but not the rest).
class Response {
String input;
List<String> keywords;
List<Node> nodes;
}
class Node {
String alias;
String name;
}
the json only has a couple of keys in it, this is largely a Json Array.
if you can change the JSON, make it more like this
{
"input" : "pol",
"keywords" : ["polaroid camera","polo",...],
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
}

Validate JSON to a Particular Format

I Have a Json which may come from other application and i need to check it whether is is in particular format. The JSON template i have is as follows,
{
"Types": {
"Type1": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3",
"recordList": {
"record1": [
{
"field": "value"
},
{
"field": {
"subrecord1": [
{
"subfield1": "subvalue1",
"subfield2": "subvalue2"
}
]
}
}
]
},
"date": "2010-08-21 03:05:03"
}
}
}
Is there any way to validate the JSON based on particular template or format.
You can use JSON Schema for that. JSON Schema lets you describe the format of the object graph you expect to receive, and then software implementing it lets you validate what you receive against your schema. There's an OSS Java implementation called json-schema-validator.

Categories