json-server - Using nested request - json

I just start using json-server and struggling with one thing. I want to have URL which are nested so e.g. to get user orgs, request would looks like:
/rest/user/orgs and will return array of user orgs
{
"rest": {
"user": {
"select": {
"org": []
},
"orgs": [{
"id": "5601e1c0-317c-4af8-9731-a1863f677e85",
"name": "DummyOrg"
}],
"logout": {}
}
}
}
Any idea what I am doing wrong?

This is not supported by the library. The way to get this working is to add a custom routes file to de server, where you will map (or redirect) requests made to /rest/user/ to /.
db.json
{
"select": {
"org": []
},
"orgs": [{
"id": "5601e1c0-317c-4af8-9731-a1863f677e85",
"name": "DummyOrg"
}],
"logout": {}
}
routes.json
{
"/rest/user/*": "/$1"
}
and then run it using json-server db.json --routes routes.json

Related

Zabbix JSONpath LLD

I am trying to make discoverig instances from AWS Auto Scaling Group.
I have json like this:
{
"AutoScalingGroups": [
{
"AutoScalingGroupName": "xxx",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-central-1:xxx",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "$Latest"
},
"MinSize": 2,
"MaxSize": 10,
"DesiredCapacity": 2,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-central-1a",
"eu-central-1c",
"eu-central-1b"
],
"LoadBalancerNames": [],
"TargetGroupARNs": [],
"HealthCheckType": "EC2",
"HealthCheckGracePeriod": 0,
"Instances": [
{
"InstanceId": "i-xxx111",
"InstanceType": "c5.2xlarge",
"AvailabilityZone": "eu-central-1b",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "11"
},
"ProtectedFromScaleIn": false
},
{
"InstanceId": "i-xxx222",
"InstanceType": "c5.2xlarge",
"AvailabilityZone": "eu-central-1a",
"LifecycleState": "InService",
"HealthStatus": "Healthy",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxx",
"LaunchTemplateName": "xxx",
"Version": "11"
},
"ProtectedFromScaleIn": false
}
]
}
]
}
And I want to create discovered items with name of instance id and values - healthstatus.
I created discovery rule with master item as previous json. And i got instance ids with item prototype that have next preprocessing JSONPath - $.AutoScalingGroups.[*].Instances.[*].InstanceId
This item has value next format ["i-xxx111","i-xxx222"], but i dont understand how to create next item prototype with names as each instance-id from this list.
So, can someone help me?
I am using zabbix version 6.2.4
You need to turn that object in the Zabbix LLD format. Using JSONpath won't help us in this case. You can use a JavaScript preprocessing in the Zabbix Item.
res = []
obj = JSON.parse(value)
for (asg in obj.AutoScalingGroups) {
for (i in obj.AutoScalingGroups[asg].Instances) {
res.push({"id": obj.AutoScalingGroups[asg].Instances[i].InstanceId})
}
}
return JSON.stringify(res)
result: [{"id":"i-xxx111"},{"id":"i-xxx222"}]
Don't forget to turn $.id in {#ID} in Zabbix LLD macro.
(Would be easier if we could use for (.. of ..) in ducktape...)
Here's a fiddle: https://jsfiddle.net/egch0vap/1/
See: https://www.zabbix.com/documentation/current/en/manual/config/items/preprocessing/javascript

Apache VTL - Copy node

Is there a way to do deep copy using apache VTL?
I was trying to use x-amazon-apigateway-integration using requestTemplates.
The input JSON is as shown below,
{
"userid": "21d6523137f6",
"time": "2020-06-16T15:22:33Z",
"item": {
"UserID" : { "S": "21d6523137f6" },
<... some complex json nodes here ...>,
"TimeUTC" : { "S": "2020-06-16T15:22:33Z" },
}
}
The requestTemplate is as shown below,
requestTemplates:
application/json: !Sub
- |
#set($inputRoot = $input.path('$'))
{
"TableName": "${tableName}",
"ConditionExpression": "attribute_not_exists(TimeUTC) OR TimeUTC > :sk",
"ExpressionAttributeValues": {
":sk":{
"S": "$util.escapeJavaScript($input.path('$.time'))"
}
},
"Item": "$input.path('$.item')", <== Copy the entire item over to Item.
"ReturnValues": "ALL_OLD",
"ReturnConsumedCapacity": "INDEXES",
"ReturnItemCollectionMetrics": "SIZE"
}
- {
tableName: !Ref EventsTable
}
The problem is, the item gets copied like,
"Item": "{UserID={S=21d6523137f6}, Lat={S=37.33180957}, Lng={S=-122.03053391}, ... other json elements..., TimeUTC={S=2020-06-16T15:22:33Z}}",
As you can see, the whole nested json become a single atribute. While I expected it to become a fully blown json node on its own like below,
"Item": {
"UserID" : { "S": "21d6523137f6" },
"Lat": { "S": "37.33180957" },
"Lng": { "S": "-122.03053391" },
<.... JSON nodes ...>
"TimeUTC" : { "S": "2020-06-20T15:22:33Z" }
},
Is it possible to deep/nested copy operation on a json node like above without doing the kung-fu of iterating the node and appending the childs o a json node variable etc...
btw, I'm using AWS API Gateway request template, so it may not support all the Apache VTL templating options.
You need to use the $input.json method instead of $input.path.
"Item": $input.json('$.item'),
Note that I removed the double quotes.
If you had the double quotes because you want to stringify $.item, you can do that like so:
"Item": "$util.escapeJavaScript($input.json('$.item'))",

ReactJS material-UI tableData to parse nested JSON

My client is ReactJS and server is Java Jersey based.
Java Jersey produces the following nested JSON.
[
{
"projectname": "BMI",
"testRun": "5934391890034305",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 21:17:34 IST 2020",
"oidobjList": [
{
"oid": "5f68cb16f01c7607230d1fcd"
},
{
"oid": "5f68cb16f01c7607230d1fcf"
},
{
"oid": "5f68cb16f01c7607230d1fd1"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
},
{
"projectname": "BMI",
"testRun": "3320691551029718",
"numberOfTestcases": "3",
"timeNowString": "Mon Sep 21 18:37:54 IST 2020",
"oidobjList": [
{
"oid": "5f68a5aaf01ca8f40b42a4e7"
},
{
"oid": "5f68a5aaf01ca8f40b42a4e9"
},
{
"oid": "5f68a5aaf01ca8f40b42a4eb"
}
],
"fileNameObjList": [
{
"fileName": "Basispath_BMI_0_out.gif"
},
{
"fileName": "Basispath_BMI_1_out.gif"
},
{
"fileName": "Basispath_BMI_2_out.gif"
}
]
}
]
My ReactJS code is as follows:
{this.props.articles.map((el, index) => (
<Table
tableHeaderColor="primary"
tableHead={["Parameter", "Value"]}
tableData={[
["Project Name: ", el.projectname],
["Test Run: ", el.testRun],
["No Of TestCases: ", el.numberOfTestcases],
// ["File Name: ", el.fileNameObjList],
["Time Stamp: ", el.timeNowString],
]}
/>
))}
The response received in client side is attached in the image
Questions:
How to render nested JSON elements in ReactJS material-ui based tableData?
How to get filenames using map and index?
Please provide solution to this issue.
1 - First you need to decide how you will render your table. If you just want to render a single table for all your nested arrays, you can generate a single array of it and render normally. You can also render multipla tables, one for each nested array, it's your choice as a programmer.
2 - You can get filenames field by doing an simple map in fileNameObjList. But if you want a single array of filenames, you can either use Array.reduce or combine the mapped arrays with Array.concat or by pushing every item with an for loop
and also, I'm not sure but i don't think Material UI table is used that way. You may be using another data-table component that uses MUI

How to set private ip using json template in Azure resource manager portal?

I am working Azure resource manager portal. i have create vm using json template. But the order of local ip address of vm is changed. `my requirement is
10.0.0.4,10.0.0.5,10.0.0.6.
but the output is
10.0.0.5, 10.0.0.6,10.0.0.4
i try to set private ip addresss using json
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "static",
"PrivateIpAddress": [concat("10.0.0."), copyindex())],
"copy": {
"name": "ipaddress",
"count": "["[variables('numberOfInstances')]"]"
},
"subnet": {
"id": "[variables('subnetRef')]"
},
"loadBalancerBackendAddressPools": [
{
"id": "[concat(variables('lbID'), '/backendAddressPools/BackendPool1')]"
}
],
"loadBalancerInboundNatRules": [
{
"id": "[concat(variables('lbID'),'/inboundNatRules/RDP-VM', copyindex())]"
}
]
}
}
]
}
},
but it throws json parsing exception. Anyone know how to set private ip address in json template?
I think the issue is with this:
"PrivateIpAddress": [concat("10.0.0."), copyindex())],
copyindex() gives you 0 with the first VM
And you cant use 10.0.0.0-3 in Azure.
Use copyindex(4)

Generate POSTMAN in webpage with JSON or something

I have a restAPI code from a programmer from JNE, company stands for delivery service.
They say that this API can be run in POSTMAN (Google Chrome Application)
It works fine in the POSTMAN, where in this application I just need to insert the request URL (which I have got from the JNE company) and two header of keys and values as follow;
KEY VALUE
----------------------------------------------
username mycompany
api key 4534645756864234523424
The method for this is POST and when I posted it, it gives me the results as how expected.
My problem now is, how can I run this code in my page, so that I don't need to run this in postman.
I am just this day going to learn JSON if anybody can help me out with this.
[UPDATE QUESTION 1]
{
"version":1,
"collections":
[
{
"id":"c8b12431-8586-cbdd-aef7-056ec177509a",
"name":"asdasdadasdasdasd",
"timestamp":1415593872130,
"requests":
[
{
"collectionId":"c8b12431-8586-cbdd-aef7-056ec177509a",
"id":"d1b2ed66-781d-d02e-c4eb-0416dd3e07a1",
"name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
"description":"",
"url":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
"method":"POST",
"headers":"username: mycompany\napi_key:089a12ffb8cd5009bdfa4ba5bdb9ee26\n",
"data":
[
{
"key":"username",
"value":"mycompany",
"type":"text"
},
{
"key":"api_key",
"value":"dsfsdfsdfs98d98sdfsdf9898dsfs",
"type":"text"
}
],
"dataMode":"params",
"timestamp":0,
"responses":[],
"version":2
}
]
}
],
"environments":[],
"headerPresets":[],
"globals":[]
}
From the update question above; my first question is: ]
In what format I have to save this file: JSON? or WHAT?
Should I save this file in one file with my webpage? or Can I save it as external file?
From the code above, I get the result as follow:
{
"detail": [
{
"code": "CGK10000",
"label": "JAKARTA"
},
{
"code": "CGK10100",
"label": "JAKARTA BARAT"
},
{
"code": "CGK10300",
"label": "JAKARTA PUSAT"
},
{
"code": "CGK10200",
"label": "JAKARTA SELATAN"
},
{
"code": "CGK10500",
"label": "JAKARTA TIMUR"
},
{
"code": "CGK10400",
"label": "JAKARTA UTARA"
}
]
}
If you have a look to the "label" it is generated from the key of the last string in the: "name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jak",
The result of the label from the last string of jak, is what I want to insert in a dropdown html tag, in where the user will choose that (the name of the location).
[Update with complete code]
POST /tracing/mycompany/origin/key/jak HTTP/1.1
Host: api.jne.co.id:8889
Content-Type: application/json
username: mycompany
api_key: 089a12ffb8cd5009bdfa4ba5bdb9ee26
{
"version":1,
"collections":
[
{
"id":"c8b12431-8586-cbdd-aef7-056ec177509a",
"name":"asdasdadasdasdasd",
"timestamp":1415593872130,
"requests":
[
{
"collectionId":"c8b12431-8586-cbdd-aef7-056ec177509a",
"id":"d1b2ed66-781d-d02e-c4eb-0416dd3e07a1",
"name":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jakarta",
"description":"",
"url":"http://api.jne.co.id:8889/tracing/mycompany/origin/key/jakarta",
"method":"POST",
"headers":"username: mycompany\napi_key:089a12ffb8cd5009bdfa4ba5bdb9ee26\n",
"data":
[
{
"key":"username",
"value":"mycompany",
"type":"text"
},
{
"key":"api_key",
"value":"089a12ffb8cd5009bdfa4ba5bdb9ee26",
"type":"text"
}
],
"dataMode":"params",
"timestamp":0,
"responses":[],
"version":2
}
]
}
],
"environments":[],
"headerPresets":[],
"globals":[]
}
I have saved this file as jne.json and jne.html but the browser just show the full code insted show the result as how the postman does. I think there are many things I am missing here.
The POST request would look something like the following
POST /tracing/mycompany/origin/key/jak HTTP/1.1
Host: api.jne.co.id:8889
Content-Type: application/json
username: mycompany
api_key: 089a12ffb8cd5009bdfa4ba5bdb9ee26
{
... your JSON ...
}
You can save JSON with the .json file extension. If your request is always the same you can save this file with your webpage, but normally an HTTP request is constructed before sending (that means you normally send different requests).
To fill the dropdown list you just have to parse the JSON response.