jq: avoid nested array mapping - jq

Here's my document:
{
"_id": "a6c8004c-efdc-4462-b474-31df2d417be3",
"address": [
{
"city": {
"value": "2505300"
},
"state": {
"value": "25"
},
"postalCode": {
"value": "25132"
}
}
]
}
{
"_id": "aa4b4ba9-32e3-4384-8cd8-0852bae0029b",
"address": [
{
"city": {
"value": "2521700"
},
"state": {
"value": "25"
},
"postalCode": {
"value": "25300"
}
}
]
}
Currently, I'm applying this JQ filter:
def pick_address:
{
city: .city | .value,
postalCode: .postalCode | .value,
state: .state | .value
};
map(.address | map(pick_address))
I'm getting this:
[
[
{
"city": "2505300",
"postalCode": "25132",
"state": "25"
}
],
[
{
"city": "2521700",
"postalCode": "25300",
"state": "25"
}
]
]
As you can see, I'm getting above objects with nested array.
I need to get it as:
[
{
"city": "2505300",
"postalCode": "25132",
"state": "25"
},
{
"city": "2521700",
"postalCode": "25300",
"state": "25"
}
]
I don't quite figure out what's wrong.
Any ideas?

Instead of mapping pick_address onto the address array
map(.address | map(pick_address))
just apply it directly onto its elements:
map(.address[] | pick_address)
Demo
On a side note: If within pick_address the operation .value is being applied to all fields, then def pick_address: map_values(.value); would be equivalent. Demo

More dynamic solution without any hardcoded keys, using
with_entries()
--slurp
jq --slurp 'map(.address[] | with_entries(.value |= .value))'
Will produce
[
{
"city": "2505300",
"state": "25",
"postalCode": "25132"
},
{
"city": "2521700",
"state": "25",
"postalCode": "25300"
}
]
As you can test in this online demo

Isn't it simpler to just do this?
jq '.[].address[] | [{city: .city.value, state: .state.value, postalCode: .postalCode.value}]'

Related

How to use ObjectSizeGreaterThan and ObjectSizeLessThan xml tags for AWS S3 command 'PutBucketLifeCycleConfiguration"?

Trying with a json input like this:
{
"Rules": [
{
"Expiration": {
"Date": "2023-01-01T00:00:00.000Z"
},
"ID": "id1",
"Filter": {
"And": {
"Tags": [
{
"Key": "k1",
"Value": "k1val1"
},
{
"Key": "k1u",
"Value": "k1vadl1"
}
],
"ObjectSizeGreaterThan": 100,
"ObjectSizeLessThan": 1000
}
},
"Status": "Enabled"
}
]
}
However, I am getting an error like this from the aws client:
Parameter validation failed:
Unknown parameter in LifecycleConfiguration.Rules[0].Filter.And: "ObjectSizeGreaterThan", must be one of: Prefix, Tags
Any idea why I get this error and how to correct it?
This seems to be working but only with the latest cli version aws-cli/2.7.30
I am getting the same error on aws-cli/1.19.39
{
"Rules": [
{
"Expiration": {
"Date": "2024-01-01T00:00:00.000Z"
},
"ID": "123",
"Filter": {
"And": {
"Prefix": "sricli",
"Tags": [
{
"Key": "ke",
"Value": "k1"
},
{
"Key": "je",
"Value": "k2"
}
],
"ObjectSizeGreaterThan": 102400,
"ObjectSizeLessThan": 204800
}
},
"Status": "Enabled"
}
]
}
Sri

Some Reverse GeoCode queries return null

We are attempting to retrieve district information for a number of locations.
However the Here API returns no values.
These locations are in the state of Arunachal Pradesh and Jammu & Kashmir in India.
I tested with a number of latitude/longitudes for these states. None of them return any data.
https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?prox=28.2180,94.7278&mode=retrieveAreas&apiKey=XXXXXXXXXXXXXXXXXXXXXXXXXXX
Response:
{
"Response": {
"MetaInfo": {
"Timestamp": "2020-08-05T15:11:46.711+0000"
},
"View": []
}
}
Even when I choose a location from Google Maps and use that in the API call, it still returns null.
https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?prox=27.083227, 93.605954&mode=retrieveAreas&apiKey=XXXXXXXXXXXXXXXXXXXXXXXXXXX
Response:
{
"Response": {
"MetaInfo": {
"Timestamp": "2020-08-05T15:11:46.711+0000"
},
"View": []
}
}
The Geocoding and Search API v7 is recommended.
https://revgeocode.search.hereapi.com/v1/revgeocode?at=28.2180%2C94.7278&lang=en-US
{
"items": [
{
"title": "Jomlo Mobuk, Arunachal Pradesh, India",
"id": "here:cm:namedplace:22803761",
"resultType": "locality",
"localityType": "city",
"address": {
"label": "Jomlo Mobuk, Arunachal Pradesh, India",
"countryCode": "IND",
"countryName": "India",
"state": "Arunachal Pradesh",
"county": "Siang",
"city": "Jomlo Mobuk",
"postalCode": "791001"
},
"position": {
"lat": 28.22394,
"lng": 94.85208
},
"distance": 0,
"mapView": {
"west": 94.72013,
"south": 28.17345,
"east": 94.9472,
"north": 28.29374
}
}
]
}

Conditional select using jq

I have this below json format, I want to take the list of "id" which satisfies the condition
in this below I want to take the id which has matchers.value as dev-stack and status.state as active
{
"status": "success",
"data": [
{
"id": "b5e7f85d",
"matchers": [
{
"name": "stack",
"value": "dev-stack",
"isRegex": true
}
],
"startsAt": "2020-07-13T07:17:36Z",
"endsAt": "2020-07-15T07:15:44Z",
"updatedAt": "2020-07-13T07:15:59.643692023Z",
"createdBy": "api",
"comment": "Silence",
"status": {
"state": "active"
}
},
{
"id": "1fdaa4b5",
"matchers": [
{
"name": "stack",
"value": "qa-stack",
"isRegex": true
}
],
"startsAt": "2020-07-10T13:19:12Z",
"endsAt": "2020-07-10T13:20:55.510739499Z",
"updatedAt": "2020-07-10T13:20:55.510739499Z",
"createdBy": "api",
"comment": "Silence",
"status": {
"state": "expired"
}
}
]
}
Here is a solution which uses update assignment |=, map and select to update .data.
Note it avoids an undesirable cartesian product if multiple .matchers meet the criteria by using any.
.data |= map(select(
(.matchers | any(.value=="dev-stack")) and (.status.state=="active")
))
Try it online!

Geocode an Address within a Bounding Box probably not working properly?

I am trying to understand how exactly bounding box is working, but from my tests at the moment it seems that result is returned regardless of the bounding box limits.
I tried various approaches,but it appears that either the flow is working this way or I am missing something. In short, I have tried to put in example request:
https://developer.here.com/api-explorer/rest/geocoder/latitude-longitude-by-mapview-parameter
mapview in Boston suburban area and search text which is my home address in Bulgaria - few thousand miles away from the bounding box borders. However, I still get a result-my address Geocoded correctly. Since it is out of the bounding box I was expecting either 0 results or some exception. Or there is some parameter in the Response I can use for defining outboxing-for this case that might be the Distnace because I am too far but in addresses close to borders I am not sure if that will be fine.
My URL request:
https://geocoder.api.here.com/6.2/geocode.json?searchtext=g.k.%20Krasna%20polyana%201%2022%D0%91%2C%201373%20g.k.%20Krasna%20polyana%201%2C%20Sofia&mapview=42.3902%2C-71.1293%3B42.3312%2C-71.0228&gen=9&app_id=devportal-demo-20180625&app_code=9v2BkviRwi9Ot26kp2IysQ
The result I got:
'{
"Response": {
"MetaInfo": {
"Timestamp": "2019-08-16T16:31:38.596+0000"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 0.88,
"Distance": 7276566.3,
"MatchLevel": "houseNumber",
"MatchQuality": {
"City": 1,
"District": 0.88,
"Street": [
0.85
],
"HouseNumber": 1,
"PostalCode": 0.56
},
"MatchType": "interpolated",
"Location": {
"LocationId": "NT_i2D3cJK.runCacYakfrAxD_yIjQ",
"LocationType": "address",
"DisplayPosition": {
"Latitude": 42.69695,
"Longitude": 23.28172
},
"NavigationPosition": [
{
"Latitude": 42.69709,
"Longitude": 23.28169
}
],
"MapView": {
"TopLeft": {
"Latitude": 42.6980742,
"Longitude": 23.2801904
},
"BottomRight": {
"Latitude": 42.6958258,
"Longitude": 23.2832496
}
},
"Address": {
"Label": "жк Красна поляна 1 22Б, 1330 София, България",
"Country": "BGR",
"County": "София-град",
"City": "София",
"District": "Красна поляна",
"Street": "жк Красна поляна 1",
"HouseNumber": "22Б",
"PostalCode": "1330",
"AdditionalData": [
{
"value": "България",
"key": "CountryName"
},
{
"value": "София-град",
"key": "CountyName"
}
]
}
}
}
]
}
]
}
}'
I am expecting some standard way to catch the results out of the bounding box. Actually, it seems that either there is no clear value to count on or I am missing something about the way it works. Thanks in advance!
I think you are looking for this one:
bbox - A type of Spatial Filter. A spatial filter limits the search for any other attributes in the request.
For your example: https://geocoder.api.here.com/6.2/geocode.json?searchtext=g.k.%20Krasna%20polyana%201%2022%D0%91%2C%201373%20g.k.%20Krasna%20polyana%201%2C%20Sofia&bbox=42.3902%2C-71.1293%3B42.3312%2C-71.0228&&app_id=yyy&app_code=xxx

How to Get County Information for the given address when we use Rest API

I am using below code to get the address information and i am getting entire address. But i only need county for the given city and zip of USA. Please help
function getCounty()
{
$.ajax({
url: 'https://geocoder.api.here.com/6.2/geocode.json',
type: 'GET',
dataType: 'jsonp',
jsonp: 'jsoncallback',
data: {
city: 'Farmington',
postalcode: '48335',
country: 'usa',
app_id: '**************************',
app_code: '************************',
gen: '9'
},
success: function (data) {
alert(JSON.stringify(data));
}
});
}
Please try below code to access County attribute of the response.
Hope this help.
alert(data.Response.View[0].Result[0].Location.Address.County);
And please check below full JSON response for the request.
{
"Response": {
"MetaInfo": {
"Timestamp": "2019-01-09T07:08:18.912+0000"
},
"View": [
{
"_type": "SearchResultsViewType",
"ViewId": 0,
"Result": [
{
"Relevance": 1,
"MatchLevel": "postalCode",
"MatchQuality": {
"City": 1,
"PostalCode": 1
},
"Location": {
"LocationId": "NT_9usvaftBv-o9T6WyVpnWRC",
"LocationType": "area",
"DisplayPosition": {
"Latitude": 42.47427,
"Longitude": -83.40889
},
"NavigationPosition": [
{
"Latitude": 42.47427,
"Longitude": -83.40889
}
],
"MapView": {
"TopLeft": {
"Latitude": 42.48732,
"Longitude": -83.4355
},
"BottomRight": {
"Latitude": 42.43954,
"Longitude": -83.37537
}
},
"Address": {
"Label": "48335, Farmington, MI, United States",
"Country": "USA",
"State": "MI",
"County": "Oakland",
"City": "Farmington",
"PostalCode": "48335",
"AdditionalData": [
{
"value": "United States",
"key": "CountryName"
},
{
"value": "Michigan",
"key": "StateName"
},
{
"value": "Oakland",
"key": "CountyName"
},
{
"value": "N",
"key": "PostalCodeType"
}
]
}
}
}
]
}
]
}
}

Resources