How to create schema for this Json data in Hive? - json

{
"filter_level": "low",
"retweeted": false,
"in_reply_to_screen_name": "paperboatdrinks",
"possibly_sensitive": false,
"truncated": false,
"lang": "und",
"in_reply_to_status_id_str": "595491441440227328",
"id": 595491657585299500,
"in_reply_to_user_id_str": "1484637054",
"timestamp_ms": "1430811249525",
"in_reply_to_status_id": 595491441440227300,
"created_at": "Tue May 05 07:34:09 +0000 2015",
"favorite_count": 0,
"place": null,
"coordinates": null,
"text": "#paperboatdrinks ������������������������������������",
"contributors": null,
"geo": null,
"entities": {
"trends": [],
"symbols": [],
"urls": [],
"hashtags": [],
"user_mentions": [
{
"id": 1484637054,
"name": "Paper Boat",
"indices": [
0,
16
],
"screen_name": "paperboatdrinks",
"id_str": "1484637054"
}
]
},
"source": "Twitter for Android",
"favorited": false,
"in_reply_to_user_id": 1484637054,
"retweet_count": 0,
"id_str": "595491657585299456",
"user": {
"location": "",
"default_profile": true,
"statuses_count": 28,
"profile_background_tile": false,
"lang": "en",
"profile_link_color": "0084B4",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/3099469722/1429881891",
"id": 3099469722,
"following": null,
"favourites_count": 2,
"protected": false,
"profile_text_color": "333333",
"verified": false,
"description": "3d visualizer",
"contributors_enabled": false,
"profile_sidebar_border_color": "C0DEED",
"name": "VICKY GUSAIN",
"profile_background_color": "C0DEED",
"created_at": "Fri Mar 20 11:43:14 +0000 2015",
"default_profile_image": false,
"followers_count": 3,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/591593652138344448/_JhM1YhS_normal.jpg",
"geo_enabled": true,
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"follow_request_sent": null,
"url": null,
"utc_offset": null,
"time_zone": null,
"notifications": null,
"profile_use_background_image": true,
"friends_count": 9,
"profile_sidebar_fill_color": "DDEEF6",
"screen_name": "VickyHanumant",
"id_str": "3099469722",
"profile_image_url": "http://pbs.twimg.com/profile_images/591593652138344448/_JhM1YhS_normal.jpg",
"listed_count": 0,
"is_translator": false
}
}`
But I had created schema but not accept it properly.
create external table if not exists paper5 (
filter_level string,
retweeted boolean,
in_reply_to_screen_name string,
possibly_sensitive boolean,
truncated boolean,
lang string,
in_reply_to_status_id_str string,
id int,
in_reply_to_user_id_str string,
timestamp_ms string,
in_reply_to_status_id int,
created_at string,
favorite_count int,
place string,
coordinates string,
text string,
contributors string,
geo string,
entities struct,
symbols: array,
urls: array,
hashtags: array,
user_mentions: array>, screen_name: string, id_str: string>>,
sources string,
favorited boolean,
in_reply_to_user_id int,
retweet_count int,
id_str string,
user struct ) ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.JsonSerde' location '/paper5/';
It showing error at entities and user_mentions field Please let me know to create a table in hive.

This link could help you it explains how you can insert data (json file) into hbase using hive QL with json_object

Related

Nested Json data to postgresql using python

How can i import my json file to a postgresql using python my data will look likes
{
"blocked_by": false,
"blocking": false,
"contributors_enabled": false,
"created_at": "Thu Dec 17 06:32:35 +0000 2020",
"default_profile": true,
"default_profile_image": false,
"description": "seo",
"entities": {
"description": {
"urls": []
}
},
"favourites_count": 12,
"follow_request_sent": false,
"followers_count": 56,
"following": false,
"friends_count": 1344,
"geo_enabled": false,
"has_extended_profile": true,
"id": 1339458394508374018,
"id_str": "1339458394508374018",
"is_translation_enabled": false,
"is_translator": false,
"lang": null,
"listed_count": 3,
"live_following": false,
"location": "",
"muting": false,
"name": "katheryn myle",
"notifications": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/1343124937389842432/30cfUmGe_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/1343124937389842432/30cfUmGe_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"protected": false,
"screen_name": "KatherynMyle",
"status": {
"contributors": null,
"coordinates": null,
"created_at": "Tue Apr 05 10:17:04 +0000 2022",
"entities": {
"hashtags": [],
"symbols": [],
"urls": [
{
"display_url": "twitter.com/i/web/status/1\u2026",
"expanded_url": "https://twitter.com/i/web/status/1511286791319564293",
"indices": [
117,
140
],
"url": "shortened url"
}
],
"user_mentions": [
{
"id": 2247373052,
"id_str": "2247373052",
"indices": [
0,
12
],
"name": "GDevelop",
"screen_name": "GDevelopApp"
}
]
},
"favorite_count": 0,
"favorited": false,
"geo": null,
"id": 1511286791319564293,
"id_str": "1511286791319564293",
"in_reply_to_screen_name": "GDevelopApp",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": 2247373052,
"in_reply_to_user_id_str": "2247373052",
"is_quote_status": false,
"lang": "en",
"place": null,
"retweet_count": 0,
"retweeted": false,
"source": "Twitter Web App",
"text": "#GDevelopApp Hello\nHow are you fine I am a link builder and we are selling links and posts on high-quality sites if\u2026 ",
"truncated": true
},
"statuses_count": 382,
"time_zone": null,
"translator_type": "none",
"url": null,
"utc_offset": null,
"verified": false,
"withheld_in_countries": []
I want to create a table with it have ing the index or key name as the column name and also i want only some index data from it not all (like utc _offset,place,etc.).so i want to insert the specific data i wanted to postgresql
https://drive.google.com/file/d/1OkJFfHyU4Eb-V3mrU2qJ_jwBaQQkunJa/view?usp=drivesdk this is the json file
Your question is very vague and I can't come up with a short answer except this one:
pSQL does support JSON as a data type:
https://www.postgresql.org/docs/current/datatype-json.html
https://www.postgresql.org/docs/current/functions-json.html
You could just store the whole json as a single json element and select/convert/whatever within postgre later on.
How to insert JSONB into Postgresql with Python?

Python 3 - Extracting value from key in nested dictionary

Hoping for some pointers here as I'm striking out with my attempts. Am working with python 3.8.5.
I'm querying a Car Park booking system that returns a json list of availability. The result has lots of nested dictionaries and I'm struggling to extract just the values i want.
This is what I've been doing:
import requests
import json
enquiry = requests.post(url.....) #queries api, this works fine
results = enquiry.text #extracts response data
dictionary = json.loads(results) #convert response to python dict
If there is one slot available, I get this output (apologies for length). If there are multiple slots available, i get the same output repeated:
{
"data": {
"services": [{
"id": null,
"name": null,
"services": [{
"id": null,
"name": "Car Park",
"met": true,
"filterCount": 1,
"primary": true,
"options": [{
"id": "9",
"images": [],
"available": true,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": null,
"startDate": "2020-08-18T13:30:00Z", <--this is what I want to extract
"endDate": "2020-08-18T14:30:00Z",
"geo": {
"lat": 0.0,
"lng": 0.0
},
"selected": false,
"linkedServices": [],
"tiers": null
}]
}],
"currentBookingId": null,
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
},
"endDate": {
"ms": 1597761000000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 15,
"days": 0
}
},
"sessionId": "2222222",
"chargeType": 1,
"hasPrimaryBookable": false,
"hasBookable": false,
"hasDiscounts": false,
"hasMultipleTiers": false,
"isPreferred": false,
"primaryServiceAvailable": true,
"primaryServiceId": null,
"primaryServiceType": "undefined",
"unavailableAttendees": []
}],
"bookingLimit": null
},
"success": true,
"suppress": false,
"version": "2.3.293",
"message": null,
"result": null,
"errors": null,
"code": null,
"flags": 0,
"redirect": null
}
I want to extract:
"startDate": "2020-08-18T13:30:00Z"
from each key, value pair in any of the returned slots. However, I cant work it out.
Extracting the whole of this nested dictionary would also contain the same data, but would then involve more work to tidy it up after.
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
I've tried loads of dictionary.get and dictionary.items variations, but cant seem to get anywhere.
I tried something like
key = ('startDate')
availability = dictionary.get(key)
print(availability)
this just returns 'none', so think im way off
Any pointers?
Thanks in advance!
Thanks for the full data. It makes testing easier :)
I had to replace null -> None, true -> True, false -> False
slot = {
"data": {
"services": [{
"id": None,
"name": None,
"services": [{
"id": None,
"name": "Car Park",
"met": True,
"filterCount": 1,
"primary": True,
"options": [{
"id": "9",
"images": [],
"available": True,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": None,
"startDate": "2020-08-18T13:30:00Z", # <--this is what I want to extract
................
print("Start Date:", slot['data']['services'][0]['services'][0]['options'][0]['startDate'])
Output
Start Date: 2020-08-18T13:30:00Z

I trying to create a DB schema based on a JSON file

I am trying to create a DB based on a JSON file but I am messing up the keys and references. Can you guys have a look and tell me whats wrong with the create statement?
The json files are basically reviews or comments from people and there are 7 response objects and one cursor object in this json file.
JSON file:
{
"cursor": {
"prev": null,
"hasNext": false,
"next": "1542275432008325:1:0",
"hasPrev": false,
"total": null,
"id": "1542275432008325:1:0",
"more": false
},
"code": 0,
"response": [
{
"editableUntil": "2018-11-22T11:20:37",
"dislikes": 0,
"numReports": 0,
"likes": 1,
"message": "<p>A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.</p>",
"id": "4196155749",
"createdAt": "2018-11-15T11:20:37",
"author": {
"username": "mnaid-233509073ed3432027d48b1a83f5fbd2",
"about": "",
"name": "baggiebuoy",
"disable3rdPartyTrackers": true,
"isPowerContributor": false,
"joinedAt": "2017-05-03T10:40:52",
"profileUrl": "https://disqus.com/by/mnaid-233509073ed3432027d48b1a83f5fbd2/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "250728493",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": null,
"isApproved": true,
"isFlagged": false,
"raw_message": "A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 1,
"moderationLabels": [],
"isEdited": true,
"sb": false
},
{
"editableUntil": "2018-11-22T10:37:59",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>This could be heaven, or this could be hell.</p>",
"id": "4196048572",
"createdAt": "2018-11-15T10:37:59",
"author": {
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"about": "",
"name": "Mordecai",
"disable3rdPartyTrackers": false,
"isPowerContributor": false,
"joinedAt": "2017-05-03T13:01:02",
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "250739494",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": 4196013534,
"isApproved": true,
"isFlagged": false,
"raw_message": "This could be heaven, or this could be hell.",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
},
{
"editableUntil": "2018-11-22T10:36:50",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>The crappest of crap deals.<br>Will never get through Parliament.</p><p>You are the weakest link May, goodbye.</p>",
"id": "4196044068",
"createdAt": "2018-11-15T10:36:50",
"author": {
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"about": "",
"name": "Mordecai",
"disable3rdPartyTrackers": false,
"isPowerContributor": false,
"joinedAt": "2017-05-03T13:01:02",
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "250739494",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": null,
"isApproved": true,
"isFlagged": false,
"raw_message": "The crappest of crap deals.\nWill never get through Parliament.\n\nYou are the weakest link May, goodbye.",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
},
{
"editableUntil": "2018-11-22T10:28:13",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>We are all just prisoners here of our own device. 😁</p>",
"id": "4196013534",
"createdAt": "2018-11-15T10:28:13",
"author": {
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"about": "",
"name": "chaffwolf",
"disable3rdPartyTrackers": true,
"isPowerContributor": false,
"joinedAt": "2017-12-20T16:45:10",
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "274657487",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": 4195981074,
"isApproved": true,
"isFlagged": false,
"raw_message": "We are all just prisoners here of our own device. 😁",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
},
{
"editableUntil": "2018-11-22T10:27:03",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>Should be no deal end off.<br>Nobody could possibly be the winner so it's simple leave and that's it .<br>Their will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,</p>",
"id": "4196012501",
"createdAt": "2018-11-15T10:27:03",
"author": {
"username": "mnaid-90ecce8d5dad4396f681182cb470872c",
"about": "",
"name": "wanderer in eire",
"disable3rdPartyTrackers": true,
"isPowerContributor": false,
"joinedAt": "2017-05-12T20:20:43",
"profileUrl": "https://disqus.com/by/mnaid-90ecce8d5dad4396f681182cb470872c/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "251694793",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": null,
"isApproved": true,
"isFlagged": false,
"raw_message": "Should be no deal end off.\nNobody could possibly be the winner so it's simple leave and that's it .\nTheir will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
},
{
"editableUntil": "2018-11-22T10:26:42",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>Damned if she does, damned if she doesn't. All for a cause she does not believe in.<br>She will go down in history whatever happens. <br>The question is: who better to lead the country at such an important juncture??<br>Answers on a postcard!</p>",
"id": "4196012237",
"createdAt": "2018-11-15T10:26:42",
"author": {
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"about": "",
"name": "chaffwolf",
"disable3rdPartyTrackers": true,
"isPowerContributor": false,
"joinedAt": "2017-12-20T16:45:10",
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "274657487",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": null,
"isApproved": true,
"isFlagged": false,
"raw_message": "Damned if she does, damned if she doesn't. All for a cause she does not believe in.\nShe will go down in history whatever happens. \nThe question is: who better to lead the country at such an important juncture??\nAnswers on a postcard!",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
},
{
"editableUntil": "2018-11-22T09:50:32",
"dislikes": 0,
"numReports": 0,
"likes": 0,
"message": "<p>Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.</p><p>You can check out any time you like, but you can never leave.</p>",
"id": "4195981074",
"createdAt": "2018-11-15T09:50:32",
"author": {
"username": "mnaid-e82a88d937e60267fd2c866b01131ada",
"about": "",
"name": "Olly the cat",
"disable3rdPartyTrackers": false,
"isPowerContributor": false,
"joinedAt": "2017-05-03T10:27:23",
"profileUrl": "https://disqus.com/by/mnaid-e82a88d937e60267fd2c866b01131ada/",
"url": "",
"location": "",
"isPrivate": false,
"signedUrl": "",
"isPrimary": true,
"isAnonymous": false,
"id": "250727584",
"avatar": {
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"isCustom": false,
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
}
}
},
"media": [],
"isSpam": false,
"isDeletedByAuthor": false,
"isDeleted": false,
"parent": null,
"isApproved": true,
"isFlagged": false,
"raw_message": "Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.\n\nYou can check out any time you like, but you can never leave.",
"isHighlighted": false,
"canVote": false,
"thread": "7046384220",
"forum": "expressandstar",
"points": 0,
"moderationLabels": [],
"isEdited": false,
"sb": false
}
]
}
SQL Statement:
CREATE TABLE cursor(
code DOUBLE NOT NULL,
hasNext VARCHAR(50) NOT NULL,
next VARCHAR(50) NOT NULL,
hasPrev VARCHAR(50) NOT NULL,
id VARCHAR(50) NOT NULL,
more VARCHAR(50) NOT NULL,
PRIMARY KEY (hasNext),
FOREIGN KEY (code) REFERENCES dbjson(code)
)
CREATE TABLE small(
isCustom VARCHAR(50) NOT NULL,
permalink VARCHAR(100) NOT NULL,
cache VARCHAR(100) NOT NULL,
PRIMARY KEY (permalink),
FOREIGN KEY (username) REFERENCES avatar(isCustom)
)
CREATE TABLE moderationLabels(
editableUntil VARCHAR(50) NOT NULL,
FOREIGN KEY (code) REFERENCES response(editableUntil)
)
CREATE TABLE large(
isCustom VARCHAR(50) NOT NULL,
permalink VARCHAR(100) NOT NULL,
cache VARCHAR(100) NOT NULL,
PRIMARY KEY (permalink),
FOREIGN KEY (username) REFERENCES avatar(isCustom)
)
CREATE TABLE response(
code DOUBLE NOT NULL,
editableUntil VARCHAR(50) NOT NULL,
dislikes DOUBLE NOT NULL,
numReports DOUBLE NOT NULL,
likes DOUBLE NOT NULL,
message VARCHAR(200) NOT NULL,
id VARCHAR(50) NOT NULL,
createdAt VARCHAR(50) NOT NULL,
isSpam VARCHAR(50) NOT NULL,
isDeletedByAuthor VARCHAR(50) NOT NULL,
isDeleted VARCHAR(50) NOT NULL,
isApproved VARCHAR(50) NOT NULL,
isFlagged VARCHAR(50) NOT NULL,
raw_message VARCHAR(200) NOT NULL,
isHighlighted VARCHAR(50) NOT NULL,
canVote VARCHAR(50) NOT NULL,
thread VARCHAR(50) NOT NULL,
forum VARCHAR(50) NOT NULL,
points DOUBLE NOT NULL,
isEdited VARCHAR(50) NOT NULL,
sb VARCHAR(50) NOT NULL,
PRIMARY KEY (editableUntil),
FOREIGN KEY (code) REFERENCES dbjson(code)
)
CREATE TABLE author(
editableUntil VARCHAR(50) NOT NULL,
username VARCHAR(50) NOT NULL,
about VARCHAR(50) NOT NULL,
name VARCHAR(50) NOT NULL,
disable3rdPartyTrackers VARCHAR(50) NOT NULL,
isPowerContributor VARCHAR(50) NOT NULL,
joinedAt VARCHAR(50) NOT NULL,
profileUrl VARCHAR(100) NOT NULL,
url VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
isPrivate VARCHAR(50) NOT NULL,
signedUrl VARCHAR(50) NOT NULL,
isPrimary VARCHAR(50) NOT NULL,
isAnonymous VARCHAR(50) NOT NULL,
id DOUBLE NOT NULL,
PRIMARY KEY (username),
FOREIGN KEY (code) REFERENCES response(editableUntil)
)
CREATE TABLE avatar(
username VARCHAR(50) NOT NULL,
isCustom VARCHAR(50) NOT NULL,
permalink VARCHAR(100) NOT NULL,
cache VARCHAR(100) NOT NULL,
PRIMARY KEY (isCustom),
FOREIGN KEY (editableUntil) REFERENCES author(username)
)
CREATE TABLE media(
editableUntil VARCHAR(50) NOT NULL,
FOREIGN KEY (code) REFERENCES response(editableUntil)
)
CREATE TABLE dbjson(
code DOUBLE NOT NULL,
PRIMARY KEY (code))
When you define foreign keys, you must define the parent table before you can define a child table that references it. Your tables are defined in an order such that several tables references their parent before the parent is created.
Next problem: cursor is a reserved keyword in MySQL. You must delimit it with back-ticks like this:
CREATE TABLE `cursor`(
...
Next problem: Several tables declare a foreign key on a column that doesn't exist in the table. For example, author has:
FOREIGN KEY (code) REFERENCES response(editableUntil)
But the author table has no column called code. The column must exist in the table before you can declare a foreign key on it. The same problem occurs in tables avatar, small, moderationLabels, large, and media.

accessing elements of json in terminal

I have a number of large json files that I need to be able to read quickly in the terminal. I am interested accessing attributes in the last element of each json, which, for example, might look like this:
},
"source": "Twitter for Android",
"text": "RT #kosaqsi_tweets: Ah ah..!!!",
"truncated": false,
"user": {
"contributors_enabled": false,
"created_at": "Tue May 06 04:48:07 +0000 2014",
"default_profile": true,
"default_profile_image": false,
"description": "",
"entities": {
"description": {
"urls": []
}
},
"favourites_count": 2147,
"follow_request_sent": false,
"followers_count": 72,
"following": false,
"friends_count": 207,
"geo_enabled": true,
"has_extended_profile": false,
"id": 2479274491,
"id_str": "2479274491",
"is_translation_enabled": false,
"is_translator": false,
"lang": "en",
"listed_count": 1,
"location": "Singapore",
"name": "karthikeyan vedalam",
"notifications": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile": false,
"profile_banner_url": "https://pbs.twimg.com/profile_banners/2479274491/1453298552",
"profile_image_url": "http://pbs.twimg.com/profile_images/748055063625605120/rByPUFsn_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/748055063625605120/rByPUFsn_normal.jpg",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"protected": false,
"screen_name": "k84362172",
"statuses_count": 1521,
"time_zone": null,
"url": null,
"utc_offset": null,
"verified": false
}
}
]
What would be the proper command in terminal to access the last element of the json file and to see, for example, the value of attribute "geo_enabled" ?
The jq command may be your friend.
At a pinch:
jq .user.geo_enabled
It'll be a slightly longer path, given your JSON structure. Probably something like:
jq .[0].parent.user.geo_enabled
(Where 'parent' is whatever is 'above' your user key).

How to get the particular value from parsed json array by using golang?

I am trying to get direct message from twitter using twitter api. I got the api json array respose like
[{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
}]
I have successfully parsed the json.But I cant get the particular data from parsed json. Here my play golang link http://play.golang.org/p/zS42Qws2Di
package main
import (
"encoding/json"
"fmt"
)
type PublicKey struct {
ID int64
ID_STR string
Text string
SENDER struct {
ID int64
ID_STR string
NAME string
}
PROTECTED bool
FOLLOWERS_COUNT int
FRIENDS_COUNT int
LISTED_COUNT int
}
type KeysResponse struct {
Collection []PublicKey
}
func main() {
s := `[{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
},{
"id": 694476444991229955,
"id_str": "694476444991229955",
"text": "Got it",
"sender": {
"id": 1690262984,
"id_str": "1690262984",
"name": "Ashok Kumar T",
"screen_name": "Ashok_kumar_T",
"location": "Trivandrum",
"description": "",
"url": null
},
"protected": false,
"followers_count": 68,
"friends_count": 32,
"listed_count": 0,
"created_at": "Thu Aug 22 06:52:53 +0000 2013",
"favourites_count": 5,
"utc_offset": 19800,
"time_zone": "Chennai",
"geo_enabled": true,
"verified": false,
"statuses_count": 124,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/378800000337984382\/5eded5a0c6fda4a85511aff15e5befd9_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1690262984\/1429709252",
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false
}]`
keys := make([]PublicKey,0)
err := json.Unmarshal([]byte(s), &keys)
if err == nil {
fmt.Printf("%+v\n", keys)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", keys)
}
myId := keys[l].Id
fmt.Printf(myId)
}
If the goal is to print the identifier of the second array element, then use this code:
myId := keys[1].ID // l changed 1, Id changed to ID
fmt.Println(myId)
playground example