Ruby JSON.parse returns an Array - ruby

I'm trying to parse a JSON string that I received from Twitter as a response via the RESTful API. I wanted to use the JSON gem, but I've run into an issue. Per the documentation, it appears that the JSON parser returns a Hash, which is what I want. However, when I actually perform the JSON.parse(twitter_result.body), it returns an Array. How can I get the JSON parser to return a Hash?
This is the parser I am using: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse
"Pretty" JSON output example:
[
{
"created_at": "Sun Dec 08 13:00:52 +0000 2013",
"id": 409668902189137920,
"id_str": "409668902189137920",
"text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.",
"source": "HootSuite",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 27500565,
"id_str": "27500565",
"name": "John Piper",
"screen_name": "JohnPiper",
"location": "Minneapolis, MN",
"description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.",
"url": "http://t.co/qt1rUlRfdl",
"entities": {
"url": {
"urls": [
{
"url": "http://t.co/qt1rUlRfdl",
"expanded_url": "http://www.desiringgod.org",
"display_url": "desiringgod.org",
"indices": [
0,
22
]
}
]
},
"description": {
"urls": [
]
}
},
"protected": false,
"followers_count": 564749,
"friends_count": 107,
[
{
"created_at": "Sun Dec 08 13:00:52 +0000 2013",
"id": 409668902189137920,
"id_str": "409668902189137920",
"text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.",
"source": "HootSuite",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 27500565,
"id_str": "27500565",
"name": "John Piper",
"screen_name": "JohnPiper",
"location": "Minneapolis, MN",
"description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.",
"url": "http://t.co/qt1rUlRfdl",
"entities": {
"url": {
"urls": [
{
"url": "http://t.co/qt1rUlRfdl",
"expanded_url": "http://www.desiringgod.org",
"display_url": "desiringgod.org",
"indices": [
0,
22
]
}
]
},
"description": {
"urls": [
]
}
},
"protected": false,
"followers_count": 564749,
"friends_count": 107,
"listed_count": 9225,
"created_at": "Sun Mar 29 20:48:44 +0000 2009",
"favourites_count": 20,
"utc_offset": -21600,
"time_zone": "Central Time (US & Canada)",
"geo_enabled": true,
"verified": true,
"statuses_count": 7906,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "570000",
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
"profile_link_color": "5C0606",
"profile_sidebar_border_color": "000000",
"profile_sidebar_fill_color": "CCC2A3",
"profile_text_color": "000000",
"profile_use_background_image": true,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 20,
"favorite_count": 31,
"entities": {
"hashtags": [
],
"symbols": [
],
"urls": [
],
"user_mentions": [
]
},
"favorited": false,
"retweeted": false,
"lang": "en"
}
] "listed_count": 9225,
"created_at": "Sun Mar 29 20:48:44 +0000 2009",
"favourites_count": 20,
"utc_offset": -21600,
"time_zone": "Central Time (US & Canada)",
"geo_enabled": true,
"verified": true,
"statuses_count": 7906,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "570000",
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg",
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg",
"profile_link_color": "5C0606",
"profile_sidebar_border_color": "000000",
"profile_sidebar_fill_color": "CCC2A3",
"profile_text_color": "000000",
"profile_use_background_image": true,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 20,
"favorite_count": 31,
"entities": {
"hashtags": [
],
"symbols": [
],
"urls": [
],
"user_mentions": [
]
},
"favorited": false,
"retweeted": false,
"lang": "en"
}
]

JSON output returns an array with one element which is the Hash you're looking for.
Try this:
puts JSON.parse(twitter_result.body)[0]

Related

setHold does not create Event as expected

I am all authenticated, and send this request to https://api.ecobee.com/1/thermostat:
{"selection":{"selectionType":"registered","selectonMatch":""},"functions":[{"type":"setHold","params":{"holdType":"indefinite","heatHoldTemp":716,"coolHoldTemp":716}}]}
I get as answer:
{
"page": {
"page": 1,
"totalPages": 1,
"pageSize": 1,
"total": 1
},
"thermostatList": [
{
"identifier": "--hidden--",
"name": "Thermostat",
"thermostatRev": "230212020022",
"isRegistered": true,
"modelNumber": "nikeSmart",
"brand": "ecobee",
"features": "Home,HomeKit",
"lastModified": "2023-02-12 02:00:22",
"thermostatTime": "2023-02-13 11:36:30",
"utcTime": "2023-02-13 16:36:30"
}
],
"status": {
"code": 0,
"message": ""
}
}
But there is no Events in my thermostat:
"events": [
{
"type": "template",
"name": "_Default_",
"running": false,
"startDate": "2037-01-01",
"startTime": "01:01:01",
"endDate": "2037-01-01",
"endTime": "02:01:01",
"isOccupied": false,
"isCoolOff": false,
"isHeatOff": false,
"coolHoldTemp": 788,
"heatHoldTemp": 644,
"fan": "auto",
"vent": "off",
"ventilatorMinOnTime": 5,
"isOptional": true,
"isTemperatureRelative": false,
"coolRelativeTemp": 40,
"heatRelativeTemp": 40,
"isTemperatureAbsolute": true,
"dutyCyclePercentage": 255,
"fanMinOnTime": 20,
"occupiedSensorActive": false,
"unoccupiedSensorActive": false,
"drRampUpTemp": 0,
"drRampUpTime": 3600,
"linkRef": "",
"holdClimateRef": "",
"fanSpeed": "low"
}
]
I am expecting, following the guide, that a Hold event is present. I do not see either a change on my thermostat UI.
Found it.
I was using HTTP GET instead of POST.
There was some typos in my JSON (selecton)
I was appending payload to URL, not as POST payload.

Error deploying config to Solana network. Error: non-base58 character

I'm testing how Metaplex's Candy Machine works and I'm having the tittle error while uploading using the CMv2-cli.
My config file is the following:
{
"price": 0,
"number": 10,
"gatekeeper": null,
"solTreasuryAccount": "<8TBP4QrwkbDEmmYbA1EJum7HN8S2c1QYvEgTPdfb35Lh>",
"splTokenAccount": null,
"splToken": null,
"goLiveDate": "27 May 2022 09:20:00 UTC",
"endSettings": null,
"whitelistMintSettings": null,
"hiddenSettings": null,
"storage": "arweave-sol",
"ipfsInfuraProjectId": null,
"ipfsInfuraSecret": null,
"nftStorageKey": null,
"awsS3Bucket": null,
"noRetainAuthority": false,
"noMutable": false
}
One of my assets file is the following:
{
"name": "Test #0001",
"symbol": "NB",
"description": "Collection of 10 numbers on the Solana blockchain.",
"seller_fee_basis_points": 500,
"image": "0.png",
"attributes": [
{"trait_type": "Layer-1", "value": "0"},
{"trait_type": "Layer-2", "value": "0"},
{"trait_type": "Layer-3", "value": "0"},
{"trait_type": "Layer-4", "value": "1"}
],
"properties": {
"creators": [{"address": "8TBP4QrwkbDEmmYbA1EJum7HN8S2c1QYvEgTPdfb35Lh ", "share": 100}],
"files": [{"uri": "0.png", "type": "image/png"}]
},
"collection": {"name": "numbers", "family": "numbers"}
}
You have 2 big errors in both files (config and the metadata).
For the config file you should not use the < > symbols, so your config file should look like this:
{
"price": 0,
"number": 10,
"gatekeeper": null,
"solTreasuryAccount": "8TBP4QrwkbDEmmYbA1EJum7HN8S2c1QYvEgTPdfb35Lh",
"splTokenAccount": null,
"splToken": null,
"goLiveDate": "27 May 2022 09:20:00 UTC",
"endSettings": null,
"whitelistMintSettings": null,
"hiddenSettings": null,
"storage": "arweave-sol",
"ipfsInfuraProjectId": null,
"ipfsInfuraSecret": null,
"nftStorageKey": null,
"awsS3Bucket": null,
"noRetainAuthority": false,
"noMutable": false
}
For the asset file you have an extra space at the end of the creators array, so you should remove that extra space from this file and I assume from the rest of ur assets too. Should look like this:
{
"name": "Test #0001",
"symbol": "NB",
"description": "Collection of 10 numbers on the Solana blockchain.",
"seller_fee_basis_points": 500,
"image": "0.png",
"attributes": [
{"trait_type": "Layer-1", "value": "0"},
{"trait_type": "Layer-2", "value": "0"},
{"trait_type": "Layer-3", "value": "0"},
{"trait_type": "Layer-4", "value": "1"}
],
"properties": {
"creators": [{"address": "8TBP4QrwkbDEmmYbA1EJum7HN8S2c1QYvEgTPdfb35Lh", "share": 100}],
"files": [{"uri": "0.png", "type": "image/png"}]
},
"collection": {"name": "numbers", "family": "numbers"}
}

Logstash doesn't filter out JSON from the Twitter API

I want to remove unnecessary fields. There are many of them. I'm using JSON filter plugin for Logstash but it doesn't work properly. It doesn't want to filter the data or just doesn't send it to the output.
I've tried to use the mutate field but without success.
I want to remove for example the entities field which is a top-level field but none of my configs are working. I also want to remove some nested fields...
Here's my example JSON from the Twitter API:
{
"retweet_count": 0,
"created_at": "Mon Dec 14 18:43:09 +0000 2020",
"place": null,
"in_reply_to_user_id_str": null,
"lang": "pl",
"filter_level": "low",
"possibly_sensitive": false,
"id": 1338555139993591800,
"id_str": "1338555139993591814",
"quote_count": 0,
"is_quote_status": false,
"geo": null,
"entities": {
"symbols": [],
"user_mentions": [],
"urls": [
{
"indices": [
117,
140
],
"url": "xxx",
"expanded_url": "xxx"
}
],
"hashtags": [
{
"text": "koronawirus",
"indices": [
84,
96
]
},
{
"text": "COVID19",
"indices": [
97,
105
]
},
{
"text": "Lockdown",
"indices": [
106,
115
]
}
]
},
"timestamp_ms": "1607971389183",
"reply_count": 0,
"retweeted": false,
"text": "W Wielkiej Brytanii wykryto nowy wariant koronawirusa. Kolejne kraje z lockdownem­čĹç\n\n#koronawirus #COVID19 #Lockdown\n\nxxx",
"contributors": null,
"truncated": false,
"in_reply_to_user_id": null,
"source": "Twitter Web App",
"#timestamp": "2020-12-14T18:43:09.000Z",
"in_reply_to_screen_name": null,
"favorited": false,
"in_reply_to_status_id": null,
"user": {
"created_at": "Tue May 12 09:11:01 +0000 2009",
"profile_use_background_image": false,
"lang": null,
"contributors_enabled": false,
"profile_text_color": "000000",
"id": 39464882,
"id_str": "39464882",
"following": null,
"geo_enabled": false,
"profile_sidebar_fill_color": "000000",
"is_translator": false,
"protected": false,
"profile_image_url": "xxx",
"profile_link_color": "3B94D9",
"name": "Salon24.pl",
"profile_sidebar_border_color": "000000",
"favourites_count": 309,
"profile_background_image_url": "xxx",
"followers_count": 17473,
"description": null,
"location": "Polska",
"url": "xxx",
"profile_background_color": "000000",
"utc_offset": null,
"profile_background_image_url_https": "xxx",
"default_profile": false,
"follow_request_sent": null,
"verified": false,
"translator_type": "none",
"friends_count": 1028,
"time_zone": null,
"default_profile_image": false,
"screen_name": "Salon24pl",
"profile_image_url_https": "xxx",
"statuses_count": 48490,
"notifications": null,
"listed_count": 203,
"profile_background_tile": false
},
"in_reply_to_status_id_str": null,
"favorite_count": 0,
"#version": "1",
"coordinates": null
}
And here's my actual config:
input {
twitter {
id => "logstash_to_kafka_plugin"
consumer_key => "xxx"
consumer_secret => "xxx"
oauth_token => "xxx"
oauth_token_secret => "xxx"
keywords => [ "koronawirus" ]
full_tweet => true
ignore_retweets => true
}
}
filter {
json {
source => "message"
remove_field => [ "[message][entities]"]
}
}
output {
kafka {
codec => json
topic_id => "twitter_tweets"
}
}
I've tried different ways to indicate that field like:
remove_field => [ "entities" ] or
remove_field => [ "[entities]" ]
but that didn't work either.
Try adding a mutate filter with remove_field after the json filter block so the new mutate filter executes after the fields have been cretaed to the root via the json filter.
Your filter could look something like
filter {
json {
source => "message"
}
mutate {
remove_field => ["entities", "user.created_at"] // works for nested field as well
}
}

'onlineMeetingUrl' is null after call Create Event in Graph api

I use create event api to create an event object with "onlineMeetingUrl: https://meet.lync.com/example/joyce/NLSD7Y62" but the response of "onlineMeetingUrl" is null.
Is it a bug or is there any way to create a new event with 'onlineMeetingUrl'?
Here are my test steps.
Step 1. POST /me/events
{
"#odata.etag": "W/\"BfXylo4WykyAenTZICXL5AAApUQ11g==\"",
"id": "AAMkADVmMzE2MjY0LTZkOGYtNGI4MS1iNWMxLTljYzg3MWY5MWQxMQBGAAAAAABZMfR36TVMQ6yunaqZPvVRBwAF9fKWjhbKTIB6dNkgJcvkAAAAAAENAAAF9fKWjhbKTIB6dNkgJcvkAAClFy2jAAA=",
"createdDateTime": "2018-03-07T02:31:20.6459109Z",
"lastModifiedDateTime": "2018-03-07T02:31:24.2554747Z",
"changeKey": "BfXylo4WykyAenTZICXL5AAApUQ11g==",
"categories": [],
"originalStartTimeZone": "Taipei Standard Time",
"originalEndTimeZone": "Taipei Standard Time",
"iCalUId": "040000008200E00074C5B7101A82E00800000000E5DACD60BCB5D301000000000000000010000000A65419D96CF4C347B8B3B5A340753291",
"reminderMinutesBeforeStart": 15,
"isReminderOn": true,
"hasAttachments": false,
"subject": "Sky Meeting",
"bodyPreview": "Join online meeting",
"importance": "normal",
"sensitivity": "normal",
"isAllDay": false,
"isCancelled": false,
"isOrganizer": true,
"responseRequested": true,
"seriesMasterId": null,
"showAs": "busy",
"type": "singleInstance",
"webLink": "https://outlook.office365.com/owa/?itemid=AAMkADVmMzE2MjY0LTZkOGYtNGI4MS1iNWMxLTljYzg3MWY5MWQxMQBGAAAAAABZMfR36TVMQ6yunaqZPvVRBwAF9fKWjhbKTIB6dNkgJcvkAAAAAAENAAAF9fKWjhbKTIB6dNkgJcvkAAClFy2jAAA%3D&exvsurl=1&path=/calendar/item",
"onlineMeetingUrl": "https://meet.lync.com/example/joyce/NLSD7Y62",
"responseStatus": {
"response": "organizer",
"time": "0001-01-01T00:00:00Z"
},
"body": {
"contentType": "html",
"content": "Join online meeting"
},
"start": {
"dateTime": "2018-03-08T00:00:00.0000000",
"timeZone": "UTC"
},
"end": {
"dateTime": "2018-03-08T00:30:00.0000000",
"timeZone": "UTC"
},
"location": {
"displayName": "Online meeting"
},
"recurrence": null,
"attendees": [],
"organizer": {
"emailAddress": {
"name": "Joyce",
"address": "joyce#example.com"
}
}
}
Step 2. The onlineMeetingUrl in the response event object is null.
{
...
"onlineMeetingUrl": null,
...
}
The onlineMeetingUrl property is not writable. We will update the documentation to reflect this.
Add these params in the request body
"isOnlineMeeting": true,
"onlineMeetingProvider": "teamsForBusiness"
onlineMeetingUrl is null in response even while passing "isOnlineMeeting": true,
"onlineMeetingProvider": "teamsForBusiness"

Duplicate ID in PUT url when editing Restangular model in ui-router custom path

currently I'm setting up restangular to edit my model. Formerly it was ok when I'm not using custom url to show the form, like
http://example.com/admin/clients (I just load the form there).
But, then I use ui-router to setup the URL like this
http://example.com/admin/clients/{clientId}/edit
When I do model.put() or model.save(), the resulting PUT url is wrong. It become like this
PUT http://referral.dev/admin/clients/f2aa1490-5a8f-11e5-b936-b3a54e0c9925/f2aa1490-5a8f-11e5-b936-b3a54e0c9925
It should be like this:
PUT http://referral.dev/admin/clients/f2aa1490-5a8f-11e5-b936-b3a54e0c9925
What I do wrong?
For convenience, here is debug from restangular object:
{
"restangularCollection": false,
"$object": {
"id": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
"referrer_id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
"email": "Claudia.Kertzmann#hotmail.com",
"first_name": "Isabelle",
"last_name": "Herzog",
"mobile": "450.357.8579x621",
"stage": "member",
"stage_at": "2015-09-09 00:00:00",
"previous_stages": [
{
"stage": "lead",
"stage_at": "2015-08-30 00:00:00"
}
],
"total_investment": 190000,
"email_token": "caf34dddfe00f36b180d459236830934",
"confirmed": false,
"unsubscribed": false,
"transfered_at": null,
"previous_referrers": [],
"created_at": "2015-09-14 03:23:20",
"updated_at": "2015-09-14 03:23:20",
"referrer": {
"id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
"email": "Eulalia.Pacocha#Dickens.biz",
"name": "Esperanza Aufderhar Sr.",
"mobile": "658.549.0277",
"role": "referrer",
"organization_id": "f0577b10-5a8f-11e5-984f-f120de15e3c1",
"last_login_at": "0000-00-00 00:00:00",
"suspended": 0
},
"route": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
"reqParams": null,
"restangularized": true,
"fromServer": true,
"parentResource": {
"route": "admin/clients",
"parentResource": null
},
"restangularCollection": false
}
}
And, here is the restangular object if don't use custom URL (which working):
{
"id": "f2aa1490-5a8f-11e5-b936-b3a54e0c9925",
"referrer_id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
"email": "Claudia.Kertzmann#hotmail.com",
"first_name": "Isabelle",
"last_name": "Herzog",
"mobile": "450.357.8579x621",
"stage": "member",
"stage_at": "2015-09-09 00:00:00",
"previous_stages": [
{
"stage": "lead",
"stage_at": "2015-08-30 00:00:00"
}
],
"total_investment": 190000,
"email_token": "caf34dddfe00f36b180d459236830934",
"confirmed": false,
"unsubscribed": false,
"transfered_at": null,
"previous_referrers": [],
"created_at": "2015-09-14 03:23:20",
"updated_at": "2015-09-14 03:23:20",
"referrer": {
"id": "f1529d80-5a8f-11e5-8ca1-21124a5fa590",
"email": "Eulalia.Pacocha#Dickens.biz",
"name": "Esperanza Aufderhar Sr.",
"mobile": "658.549.0277",
"role": "referrer",
"organization_id": "f0577b10-5a8f-11e5-984f-f120de15e3c1",
"last_login_at": "0000-00-00 00:00:00",
"suspended": 0
},
"route": "admin/clients",
"reqParams": null,
"restangularized": true,
"fromServer": true,
"parentResource": null,
"restangularCollection": false
}
I've tried manually set $object.route to "admin/clients" but the PUT url is still the same.
Thanks.. :)
I've solved it. Formerly I was using:
var models = Restangular.all('admin/clients');
Then use
models.one($stateParams.clientId).get()
to get the model.
The correct way, is use Restangular.one to load model, eg. like this:
vm.model = Restangular.one('admin/clients', $stateParams.clientId).get();

Resources