I have to create an API that can return JSON object in two different formats based on the input parameter. Suppose if we pass "report=monthly" in query string the output will be like:
[{
"Month": "Jan",
"Details": [{
"userId": 12345,
"userName": "ABC"
}]
}, {
"Month": "Feb",
"Details": [{
"userId": 12346,
"userName": "ABD"
}]
}]
If the above parameter is not passed the output will be like:
{
"Details": [{
"userId": 12345,
"userName": "ABC"
}, {
"userId": 12346,
"userName": "ABD"
}]
}
How can I define schema for a single API to return JSON in the above formats? I can not create 2 endpoints.
The easier way is to define Month as an optional property.
paths:
/report:
parameters:
...
get:
responses:
200:
description: OK
schema:
type: array
items:
$ref: '#/definitions/ReportEntry'
definitions:
ReportEntry:
type: object
properties:
Month:
type: string
example: Jan
Details:
type: array
items:
$ref: '#/definitions/UserInfo'
required:
- Details # <---- "Details" is required, "Month" is optional
UserInfo:
type: object
properties:
userId:
type: integer
example: 12345
userName:
type: string
example: ABC
required:
- userId
- userName
Related
Using Loki and Promtail and I have some JSON which I am looking to parse with a pipeline stage. I have defined expressions and created a new JSON stage for every nested object/array, however the logs that I am parsing do not have a definite number of objects in an array. It could be 2, it could be 5.
Here is some example JSON
{
"level": "info",
"msg": "This is the message",
"stats": {
"bytes": 12345,
"checks": 1234,
"transferring": [
{
"eta": 1234,
"name": "path/to/file/1"
},
{
"eta": 1234,
"name": "path/to/file/2"
},
{
"eta": 1234,
"name": "path/to/file/3"
},
{
"eta": 1234,
"name": "path/to/file/4"
},
{
"eta": 1234,
"name": "path/to/file/5"
}
],
"transfers": 28
},
"time": "2022-07-28T13:55:47.251259+01:00"
}
Here is my pipeline_stage:
pipeline_stages:
- json:
expressions:
level: level
msg: msg
timestamp: time
stats:
- json:
expressions:
bytes: bytes
checks: checks
transferring:
source: stats
- json:
expressions:
transferring_eta: eta
transferring_name: name
source: transferring
- labels:
level:
msg:
timestamp:
stats:
bytes:
checks:
transferring:
transferring_eta:
transferring_name:
The "bytes" and "checks" key-value pairs get created, as they are nested inside the "stats" value, however I can not create the "transferring_eta" or "transferring_name" key-value pairs.
I am failing to to remove the outer array from the json request body that I want swagger to generate from the schema definitions.
I want to generate this object:
{
"name":
{
"value": "test04"
},
"mail": {
"value": "test04#gmail.com"
}
}
But swagger is giving me this:
[
{
"name": {
"value": "string"
},
"mail": {
"value": "string"
}
}
]
This is my definition section:
definitions:
user:
type: "object"
properties:
name:
type: object
properties:
value:
type: string
mail:
type: object
properties:
value:
type: string
May you kindly assist. I am new to swagger. I am using version 2.0
Your User schema is correct, so this means there's an extra type: array somewhere in the parameter, request body, or response where this schema is used.
I have the following documents in MongoDB:
{
name: "document1",
data: [{
name: "xxxxx",
status: "delivered"
},{
name: "xxxxx",
status: "In Process"
},{
name: "xxxxx",
status: "Not Started"
}]
},{
name: "document2",
data: [{
name: "xxxxx",
status: "delivered"
},{
name: "xxxxx",
status: "delivered"
},{
name: "xxxxx",
status: "delivered"
}]
}
I want to find all documents where all objects inside the "data" array have the status "delivered", I used to use $ne: 'Not Started', but now that more status options have been introduced, this doesn't work. Perhaps I could do a $ne: $and and include all negative options, but any new status will give issues.
You can use $not with $elemMatch
db.collection.find({
"data": {
"$not": {
"$elemMatch": {
"status": {
$ne: "delivered"
}
}
}
},
"data.status": "delivered"
})
Here is fiddle https://mongoplayground.net/p/digdoFC6yF0
You can use $not.
{
$and: [
{ 'data.status': { $not: /Not Started/},
{'data.status': "delivered"}
]
}
Note that $not must receive a regex and not just plain string.
I have a following json string:
[ { id: '123', name: 'bla bla', type: 'Source', isLeaf: true },
{ id: '3425', name: 'test test', type: 'Reference', isLeaf: false },
{ id: '12678', name: 'tags', type: 'Source', isLeaf: false },
]
I am trying to parse this using JsonSlurper but getting error:
groovy.json.JsonException: Lexing failed on line: 1, column: 5, while reading 'i', no possible valid JSON value or punctuation could be recognized.
How do I parse it and access the id:'3425'?
Your Json is invalid, you need to use double quote to delimit strings and you also need to put quotes around the keys in your Json like so:
[ { "id": "123", "name": "bla bla", "type": "Source", "isLeaf": true },
{ "id": "3425", "name": "test test", "type": "Reference", "isLeaf": false },
{ "id": "12678", "name": "tags", "type": "Source", "isLeaf": false },
]
You can then do:
def ids = new groovy.json.JsonSlurper().parseText( json ).id
assert ids[ 1 ] == '3425'
I have a pretty nested JSON coming from a ldap_search() call. I would like to use this information to populate an ExtJS ComboBox, but I am facing some troubles with the reader. Apparently, I am not able to read the information that I need in the ComboBox, that is the mail address of the people, the uid and the cn
I think the whole problem lies in the store. I was trying the following code:
var store= new Ext.data.JsonStore({
url:'search.php',
root: '',
totalProperty: 'count',
fields: [
{name:'cn', type: 'string', mapping:'cn.0'},
{name:'mail', type: 'string', mapping:'mail.0'},
{name:'uid', type: 'string', mapping:'uid.0'}
]
});
but FireBug told me missing ; before statement return obj.cn.0 in ext-all.js (line 7). I tried with another, easier JSON array and it works, that is why I really think the problem lies in this part of code, especially in the mapping.
an example of JSON returned by search.php is:
{
"count": 2,
"0": {
"mail": {
"count": 1,
"0": "Mail address not registered."
},
"0": "mail",
"uid": {
"count": 1,
"0": "name0.surname0#domain.com"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname0 Name0"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname0 Name0,ou=personal,dc=domain,dc=com"
},
"1": {
"mail": {
"count": 1,
"0": "name1.surname1#domain.com"
},
"0": "mail",
"uid": {
"count": 1,
"0": "name1.surname1"
},
"1": "uid",
"cn": {
"count": 1,
"0": "Surname 1 Name 1"
},
"2": "cn",
"count": 3,
"dn": "cn=Surname1 Name1,ou=personal,dc=domain,dc=com"
}
}
Thanks for your time.
Yep, that JSON structure is not going to work straight away with standard ExtJS JSONReader. Take a look at this example taken from the ExtJS API documentation on how the JSON should look like.
{
results: 2000, // Reader's configured totalProperty
rows: [ // Reader's configured root
// record data objects:
{ id: 1, firstname: 'Bill', occupation: 'Gardener' },
{ id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
...
]
}
Also, the root config option is required, you cannot leave it empty. In the above example your root would be "rows".
You are probably going to need to parse that JSON of yours into a simpler format at first, before feeding it to the JSONReader.
I was looking to do the same thing, but have one of the nested items be a field in my chart. This post kept coming up, so I thought it might be helpful to see what I did to solve the chart issue. The key to solving it is knowing that the label config exists: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.chart.Label. Using that you can override the default render of what you pass in. In this example the field is "key" (Not shown here, but my model is using the default type for 'key' (ie., not string)). The key object gets passed to renderer. Using function(t), I can now access that object like javascript and pass back the name under the object.
json
key : {
wholePath : "c:/.../fileName.txt",
fileName : "fileName.txt",
}
code:
axes: [
{
title: 'Values',
type: 'Numeric',
position: 'left',
fields: ['value'],
minimum: 0,
maximum: 100,
minorTickSteps: 1
},
{
title: 'File Name',
type: 'Category',
position: 'bottom',
fields: ['key'],
label: {
renderer: function(t) {
var fileName = t.name;
return fileName;
}
}
}