Different REST representations of the same resource - json

I have a following situation when designing Rest.
For example I have a list of daily prices
[
{"id": 50,
"date": "2018-01-05"},
{"id": 60,
"date": "2018-01-06"},
{"id": 70,
"date": "2018-01-10"}
]
First I want to get all the prices in certain period, for example in January with GET /prices/?startDate=2018-01-01&endDate=2018-01-31 and it would return the results as seen above.
Secondly I want to get prices for the the same period but with price=0 where no price exists, such as
[
{"price": 50,
"date": "2018-01-05"},
{"price": 60,
"date": "2018-01-06"},
{"price": 0,
"date": "2018-01-07"},
{"price": 0,
"date": "2018-01-08"},
{"price": 0,
"date": "2018-01-09"},
{"price": 70,
"date": "2018-01-10"},
{"price": 0,
"date": "2018-01-11"}
]
Could I go with a new endpoint for this such as /prices/in-range/?startDate=2018-01-01&endDate=2018-01-31.
Would that be misleading? Should REST return data that is non-existing such as price=0, or should it be left to client to maniuplate original data?
Is there a naming convention in REST for "derived" data from one resource to another? I am not choosing what data to display, I am basically creating new data here.

Related

Query All Elements in Nested JSON Array PostrgreSQL

I am trying to create a query in SQL to retrieve DNS answer information so that I can visualize it in Grafana with the add of TimescaleDB. Right now, I am struggling to get postgres to query more than one element at a time. The structure of my JSON that I am trying to query looks like this:
{
"Z": 0,
"AA": 0,
"ID": 56559,
"QR": 1,
"RA": 1,
"RD": 1,
"TC": 0,
"RCode": 0,
"OpCode": 0,
"answer": [
{
"ttl": 19046,
"name": "i.stack.imgur.com",
"type": 5,
"class": 1,
"rdata": "i.stack.imgur.com.cdn.cloudflare.net"
},
{
"ttl": 220,
"name": "i.stack.imgur.com.cdn.cloudflare.net",
"type": 1,
"class": 1,
"rdata": "104.16.30.34"
},
{
"ttl": 220,
"name": "i.stack.imgur.com.cdn.cloudflare.net",
"type": 1,
"class": 1,
"rdata": "104.16.31.34"
},
{
"ttl": 220,
"name": "i.stack.imgur.com.cdn.cloudflare.net",
"type": 1,
"class": 1,
"rdata": "104.16.0.35"
}
],
"ANCount": 13,
"ARCount": 0,
"QDCount": 1,
"question": [
{
"name": "i.stack.imgur.com",
"qtype": 1,
"qclass": 1
}
]
}
There can be any number of answers, including zero, so I would like to figure out a way to query all answers. For example, I am trying to retrieve the ttl field from every index answer, and I can query a specific index, but have trouble querying all occurrences.
This works for querying a single index:
SELECT (data->'answer'->>0)::json->'ttl'
FROM dns;
When I looked around, I found this as a potential solution for querying all indices within the array, but it did not seem to work and told me "cannot extract elements from a scalar":
SELECT answer->>'ttl' ttl
FROM dns, jsonb_array_elements(data->'answer') answer, jsonb_array_elements(answer->'ttl') ttl
Using jsonb_array_elements() will give you a row for every object in the answer array. You can then dereference that object:
select a.obj->>'ttl' as ttl, a.obj->>'name' as name, a.obj->>'rdata' as rdata
from dns d
cross join lateral jsonb_array_elements(data->'answer') as a(obj)

Classroom API returns wrong due date

I am requesting for a list of assignment for each course. For this example I created the due date to be on the 11th but the API returns the due date as the 12th.
courseWork": [
{
"courseId": "116315138435",
"id": "116726071520",
"title": "Test Assignments",
"description": "ojoijoijoijoijoij",
"state": "PUBLISHED",
"alternateLink": "https://classroom.google.com/c/MTE2MzE1MTM4NDM1/a/MTE2NzI2MDcxNTIw/details",
"creationTime": "2020-07-09T17:53:00.220Z",
"updateTime": "2020-07-09T17:53:10.544Z",
"dueDate": {
"year": 2020,
"month": 7,
"day": 12
},
"dueTime": {
"hours": 4,
"minutes": 59
},
"maxPoints": 100,
"workType": "ASSIGNMENT",
"submissionModificationMode": "MODIFIABLE_UNTIL_TURNED_IN",
"creatorUserId": "111094682610866207024"
}
]
I am not sure that I can use a time zone to fix this because it is simply returning the day/month/year without a specified time zone. Can I simply subtract the due dates day by one? or is there a more logical approach?

How can I insert following JSON in MongoDB collection as different documents

I need to insert data in mongo but the JSON I am getting has multiple values in every field and I don't know how can I split them to insert in different documents.
I want to insert array data in different objects in MongoDB
{
"activity_template_id": [
1,
2,
3,
4,
5,
7
],
"done_date": [
"2019-08-10",
"2019-08-10",
"2019-08-10",
"0000-01-01",
"0000-01-01",
"0000-01-01"
],
"is_prescribed": [
"N",
"N",
"N",
"N",
"N",
"Y"
],
"material_id": [
1,
5,
21,
10,
14,
0
],
"qty": [
"1",
"1",
"1",
"0",
"0",
"0"
],
"unit_id": [
1,
1,
25,
0,
0,
0
],
}
(As far as I know) there is no feature in MongoDB itself that would process input data like that. You would do that in application code before calling MongoDB.
If there is no separate application, you can use standard JavaScript functions within the mongo shell to do that.

reading and editing json file

I am new to Python and trying to edit a .json file that looks like the dictionary below:
{
"name": "MB_NDE_AX667_ECU[500-2000]",
"physical_quantity": "acceleration",
"unit": "m/s2",
"okrangelow": 0,
"okrangehigh": 10,
"input": "ch2_ECU1.rms",
"history": {
"ds": 30,
"timer": 24,
"files": 30
},
"reg": 44
},
My goal is to get another json file that will have the dictionary restructured to single line dictionary, e.g:
{"name":"MB_NDE_AX667_ECU[500-2000]","physical_quantity":"acceleration",......}
How could I do this?
Thanks

Stream Analytics: performing a JOIN on events and ref. data in JSON

I'd like to perform a JOIN query on incoming JSON events and JSON reference data in Azure Stream Analytics.
The JSON events look like the following:
{"devicename":"12345","value":25}
The JSON Reference data (simplified) looks like the following:
{
"Id": "configuration",
"Sites": [{
"Id": "b83939b5-6502-4140-b807-205162ac4939",
"Name": "site1",
"Location": {
"Latitude": 5.000,
"Longitude": 5.000
},
"Lines": [{
"Id": "e707a451-948a-498e-80de-d61bc448a5ef",
"Name": "line1",
"Sections": [{
"Id": "d17d762c-4291-4912-9dcf-72113c9f0b4b",
"Name": "section1",
"Sensors": [{
"Id": "S1_PL1_LS1_M1_device_001",
"Name": "sensor1",
"ExpectedAVG": 55,
"ExpectedMIN": 35,
"ExpectedMAX": 75,
"TypeOfSensor": "Humidity"
}, {
"Id": "S1_PL1_LS1_M1_device_002",
"Name": "sensor2",
"ExpectedAVG": 105,
"ExpectedMIN": 90,
"ExpectedMAX": 118,
"TypeOfSensor": "Temperature"
}]
}]
}]
}]
}
Now I'd like to perform a JOIN on the incoming device data and the reference data.
SELECT
i.devicename as pk,
i.value as rk,
system.timestamp as time,
r.Sites.Lines.Sections.Sensors.Temperature as Temperature,
r.Sites.Lines.Sections.Sensors.Humidity as Humidity
INTO
output
FROM
IoTHubStream i
INNER JOIN
Reference r ON r.Sites.Lines.Sections.Sensors.Id = i.devicename
Now how can this be done? The problem is that the reference data can change and I would like to handle this dynamically.
I'm thinking about using a WHERE statement but I don't know how this could look like, some advice/help is really welcome.
Thanks in advance!
You can access the array elements using GetArrayElements described in this page https://msdn.microsoft.com/en-us/library/azure/mt298451.aspx
If your data changes make it impossible to use GetArrayElements, please share details about how it changes.