I made an api query which returns a json as response. I am trying to extract the temperature_2m for the first time in the list (2022-11-03T00:00) which is 5.7, not sure how to get it with python
api_query ={
"latitude": 52.52,
"longitude": 13.419998,
"generationtime_ms": 0.36203861236572266,
"utc_offset_seconds": 0,
"timezone": "GMT",
"timezone_abbreviation": "GMT",
"elevation": 38.0,
"hourly_units": {
"time": "iso8601",
"temperature_2m": "°C"
},
"hourly": {
"time": [
"2022-11-03T00:00",
"2022-11-03T01:00"
],
"temperature_2m": [
5.7,
5.2
]
}
}
for key in api_query:
temperature = api_query['hourly']['time'][0]['temperature_2m']
print(temperature)
This is not working:
temperature = api_query['hourly']['time'][0]['temperature_2m']
coz temperature is a sub attribute from "hourly" but not from "time"
you need instead:
for the temp:
temperature = api_query['hourly']['temperature_2m'][0]
print(temperature)
and for the time:
time = api_query['hourly']['time'][0]
here a Jupyter Notebook
temperature = api_query['hourly']['temperature_2m'][0]
Related
My SQL skills are very limited. Please help.
Modbus module on the Azure IoT edge returns JSON content to Stream Analytics job in this format (from downloaded sample data) -
[
{
"PublishTimestamp": "2021-07-28 19:28:15",
"Content": [
{
"HwId": "XY-MOD2-1",
"Data": [
{
"CorrelationId": "DefaultCorrelationId",
"SourceTimestamp": "2021-07-28 19:28:15",
"Values": [
{
"DisplayName": "Temperature",
"Address": "30002",
"Value": "210"
},
{
"DisplayName": "Temperature",
"Address": "30003",
"Value": "538"
}
]
}
]
}
],
"EventProcessedUtcTime": "2021-07-28T20:26:23.9127084Z",
"PartitionId": 0,
"EventEnqueuedUtcTime": "2021-07-28T19:28:15.9460000Z",
"IoTHub": {
"MessageId": null,
"CorrelationId": null,
"ConnectionDeviceId": "rp4linuxedge1",
"ConnectionDeviceGenerationId": "637630846187016425",
"EnqueuedTime": "2021-07-28T19:28:15.9550000Z",
"StreamId": null
}
},
I am unable to figure out what SQL syntax should I use to get this as output -
SourceTimestamp
Address
Value
Time1
30002
210
Time1
30003
538
Time2
30002
215
Time2
30003
540
The array within the JSON object can be accessed using this block of codes -
select
cast(dataArr.ArrayValue.SourceTimestamp as datetime) as SourceTimestamp,
cast(valuesArr.ArrayValue.Address as bigint) as Address,
cast(valuesArr.ArrayValue.Value as float) as Value
into powerbioutput
from iotinput i
cross apply GetArrayElements(i.Content) as contentArr
cross apply GetArrayElements(contentArr.ArrayValue.Data) as dataArr
cross apply GetArrayElements(dataArr.ArrayValue.[Values]) as valuesArr
I have captured two sets of values by using JSON extractor in JMeter which I want to concatenate. Let me give you an example below for the format which I want to use.
The following are the two sets of captured values:
Set 1: [V2520 V2522 V2521 V2500 V2500]
Set 2: [PL PL PL NP NP]
So from the above sets, I am looking for the something like the following value, because the body which I have to send in a subsequent call contains the combination of these 2 values:
Answer: ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Can you please help me how to solve this in JMeter using Groovy?
This is the JSON I have:
{ "body": {
"responseObject": [
{
"benefitInfo": [
{
"procedureCode": "V2520",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposable",
"id": "96",
"coPayAmount": "25"
},
{
"procedureCode": "V2522",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposableBifocal",
"id": "98",
"coPayAmount": "25"
},
{
"procedureCode": "V2521",
"modifier": "PL",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensDisposableToric",
"id": "97",
"coPayAmount": "25"
},
{
"procedureCode": "V2500",
"modifier": "NP",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLens (Non Plan)",
"id": "89",
"coPayAmount": "0"
},
{
"procedureCode": "V2500",
"modifier": "NP",
"usage": "Dress",
"authorizationID": null,
"description": "ContactLensConventional (Non Plan)",
"id": "157",
"coPayAmount": "0"
}
]
}
]}}
An easy way to do this is to combine them as you collect the values from the JSON when you parse it.
def json = new groovy.json.JsonSlurper().parseText(text)
def answer = json.body.responseObject[0].benefitInfo.collect { it.procedureCode + it.modifier }
assert answer == ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Another method would be to use transpose() and join():
def r = new groovy.json.JsonSlurper().parseText(text).body.responseObject.benefitInfo[0]
def answer = [r.procedureCode, r.modifier].transpose()*.join()
assert answer == ["V2520PL", "V2522PL", "V2521PL", "V2500NP", "V2500NP"]
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def answer = []
def benefitInfos = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..benefitInfo')
benefitInfos.each { benefitInfo ->
benefitInfo.each { entry ->
answer.add(entry.get('procedureCode') + entry.get('modifier'))
}
}
vars.put('answer', new groovy.json.JsonBuilder(answer).toPrettyString())
That's it, you will be able to access generated value as ${answer} where required:
References:
Jayway JsonPath
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I convert excel file to JSON , to import it into my firebase DB.
On conversion, I have the JSON data in below format
[
{
"ProductNumber": "7381581",
"SKU": "test3",
},
{
"ProductNumber": "7381582",
"SKU": "test",
},
{..}
]
But I need it like this
{
"7381581" :{
"ProductNumber": "7381581",
"SKU": "test3",
},
"7381582":{
"ProductNumber": "7381582",
"SKU": "test",
},{..}
}
How can I make changes to the spreadsheet records to get the JSON in the above format ? (OR)
How should I add the key values to JSON dynamically?
You can use reduce as suggested to iterate over the original array and transform it into an object.
data.reduce((prev, current) => {
prev[current.ProductNumber] = current;
return prev;
}, {});
You can see a working example in the playground here.
Was trying to extract values from a parsed json object using a function node or split & switch node, tried to many ways and nothing seems to work fro me.
the example below I would like to have 5 outputs for the text and numeric values.
here is my payload in json:
{
"applicationID": "1",
"applicationName": "test_ds18b20",
"deviceName": "arduino_uno",
"devEUI": "1234567890123456",
"rxInfo": [
{
"mac": "aa755a0048050130",
"rssi": -57,
"loRaSNR": 10,
"name": "raspberry_pi",
"latitude": 1.466860686785175,
"longitude": 2.019478797912605,
"altitude": 0
}
],
"txInfo": {
"frequency": 868100000,
"dataRate": {
"modulation": "LORA",
"bandwidth": 125,
"spreadFactor": 7
},
"adr": true,
"codeRate": "4/5"
},
"fCnt": 9,
"fPort": 1,
"data": "Z29vZGJ5ZQ==",
"object": {}
}
first i try with function node to extract "data", but it returns array like this:
0: ""data":"Z29vZGJ5ZQ==""
1: "Z29vZGJ5ZQ=="
i dont need array i need string
function:
var regexsearch = /\"data\":\"(.*?)\"/i;
var my = msg.payload.match(regexsearch);
msg.payload = my;
return msg;
but i need to get only this Z29vZGJ5ZQ==
than i try with split & switch nodes and gets the whole linelike this: ""data":"Z29vZGJ5ZQ==""
but i need to get only this Z29vZGJ5ZQ==
and here is my flow:
[{"id":"d46d38e2.27cc78","type":"inject","z":"ff592a31.cf21a8","name":"","topic":"","payload":"{\"applicationID\":\"1\",\"applicationName\":\"test_ds18b20\",\"deviceName\":\"arduino_uno\",\"devEUI\":\"1234567890123456\",\"rxInfo\":[{\"mac\":\"aa755a0048050130\",\"rssi\":-57,\"loRaSNR\":10,\"name\":\"raspberry_pi\",\"latitude\":48.466860686785175,\"longitude\":35.019478797912605,\"altitude\":0}],\"txInfo\":{\"frequency\":868100000,\"dataRate\":{\"modulation\":\"LORA\",\"bandwidth\":125,\"spreadFactor\":7},\"adr\":true,\"codeRate\":\"4/5\"},\"fCnt\":9,\"fPort\":1,\"data\":\"Z29vZGJ5ZQ==\",\"object\":{}}","payloadType":"json","repeat":"","crontab":"","once":false,"x":90,"y":160,"wires":[["1a34819e.743eee"]]},{"id":"105db6d9.0df1c9","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":100,"wires":[]},{"id":"1ac8a3e1.8f379c","type":"split","z":"ff592a31.cf21a8","name":"","splt":",","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":250,"y":340,"wires":[["c10ec515.102d38"]]},{"id":"c10ec515.102d38","type":"switch","z":"ff592a31.cf21a8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"\"data\":","vt":"str"},{"t":"cont","v":"\"latitude\":","vt":"str"}],"checkall":"true","outputs":2,"x":370,"y":340,"wires":[["105db6d9.0df1c9"],["6b2d5d19.7868e4"]]},{"id":"1a34819e.743eee","type":"json","z":"ff592a31.cf21a8","name":"","pretty":false,"x":115.55555555555556,"y":312.22222222222223,"wires":[["1ac8a3e1.8f379c","bae9fa5d.a9f238"]]},{"id":"bae9fa5d.a9f238","type":"function","z":"ff592a31.cf21a8","name":"match","func":"var regexsearch = /\\\"data\\\":\\\"(.*?)\\\"/i;\nvar my = msg.payload.match(regexsearch);\nmsg.payload = my;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":160,"wires":[["105db6d9.0df1c9"]]},{"id":"6b2d5d19.7868e4","type":"debug","z":"ff592a31.cf21a8","name":"","active":true,"console":"false","complete":"false","x":610,"y":180,"wires":[]}]
Thanks for help
my json data:
[
{
"Code": "GB-00001",
"BasicInformation": {
"WGS84Longitude": -4.670000,
"WGS84Latitude": 50.340000
},
"Availability": [{
"ArrivalDate": "2017-04-21",
"Price": 689
},
{
"ArrivalDate": "2017-04-28",
"Price": 1341
}
]},
{
"Code": "GB-00002",
"BasicInformation": {
"WGS84Longitude": -4.680000,
"WGS84Latitude": 50.350000
},
"Availability": [{
"ArrivalDate": "2017-04-21",
"Price": 659
},
{
"ArrivalDate": "2017-04-28",
"Price": 1440
}
]}
}]
I'd like the result to be like:
[
{
"HouseCode": "GB-00001",
"Country": "GB",
"location": {
"type": "Point",
"coordinates": [
50.340000,
-4.670000
]
}, "lowestPrice": 689
},
{
"HouseCode": "GB-00002",
"Country": "GB",
"location": {
"type": "Point",
"coordinates": [
50.350000,
-4.680000
]
}, "lowestPrice" : 659
}
My problem is: how to use the min(c.Availability.Price)
This is my current query with the lat lng convert to point, but no idea how to get the minimum/lowest price.
SELECT c.Code, c.BasicInformation.Country ,
{"type":"Point","coordinates": [c.BasicInformation.Latitude, c.BasicInformation.Longitude]} as location
FROM c
already tried with Join c.Availability a and , min(a.Price)
edit perhaps I am too early? https://feedback.azure.com/forums/263030-documentdb/suggestions/18561901-add-group-by-support-for-aggregate-functions
found that url in https://stackoverflow.com/a/42697673/169714
This is a pretty close to ideal situation for a user defined function (UDF).
Here is one that should do the trick:
function lowestPrice(availability) {
var i, len, lowest, row;
lowest = 2e308;
for (i = 0, len = availability.length; i < len; i++) {
row = availability[i];
lowest = Math.min(lowest, row.Price);
}
return lowest;
};
You call it like this:
SELECT
c.Code,
c.BasicInformation.Country,
{"type":"Point","coordinates": [
c.BasicInformation.Latitude, c.BasicInformation.Longitude
]} as location,
udf.lowestPrice(c.Availability) as lowestPrice
FROM c
AFAIK, you could only use UDF to achieve your requirement for now. Also, I have checked the code provided by Larry Maccherone, and it could both work on Azure DocumentDB service and my DocumentDB Emulator (version 1.11.136.2) as follows:
DocumentDB.GatewayService.exe has stopped working
For DocumentDB.GatewayService crash, I assumed that you need to collect the dump files and attach them with an email to askdocdb#microsoft.com. For more details, you could refer to DocumentDB Emulator troubleshooting.