2017-07-27 41 views
1

我正在研究一些API信息收集和我已經編寫了一個程序,它讀取域並查找API信息,並且在它找到信息後,它將它製作成的整個JSON文本放入列在Microsoft SQL服務器表中。解析JSON在多列

這是怎樣的JSON輸出的一個樣子:

{ 
    "id": "3e3562a7-b160-4fd8-a190-8ca0a5288794", 
    "name": "Garmin", 
    "legalName": "Garmin Ltd.", 
    "domain": "garmin.com", 
    "domainAliases": [ 
    "garmin.de", 
    "garmin.se", 
    "garmin.si", 
    "garmin.nl", 
    "garmin.dk", 
    "garmin.it", 
    "garmin.fi", 
    "garmin.no", 
    "garmin.hr", 
    "garmin.at", 
    "garmin.pl", 
    "garmin.cl", 
    "garmin.com.au", 
    "garmin.co.uk", 
    "garmin.be", 
    "garminconnect.com", 
    "garmin.com.mx", 
    "garminservice.de", 
    "garmin.es", 
    "garminasus.com", 
    "garminonline.de", 
    "opencaching.com", 
    "garmin.com.br", 
    "garmin.pt", 
    "garminfrance.com", 
    "garmin.ro", 
    "garmin.com.hr", 
    "garmin.com.ar", 
    "garmin.ca", 
    "inreachdelorme.com" 
    ], 
    "site": { 
    "title": "Garmin International | Home", 
    "h1": "NEW VIRB® 360", 
    "metaDescription": "Delivering innovative GPS technology across diverse markets, including aviation, marine, fitness, outdoor recreation, tracking and mobile apps.", 
    "metaAuthor": "Garmin", 
    "phoneNumbers": [ 
     "+1 913-397-8200" 
    ], 
    "emailAddresses": [ 
     "[email protected]", 
     "[email protected]", 
     "[email protected]", 
     "[email protected]", 
     "[email protected]" 
    ] 
    }, 
    "category": { 
    "sector": "Information Technology", 
    "industryGroup": "Software & Services", 
    "industry": "Internet Software & Services", 
    "subIndustry": "Internet Software & Services", 
    "sicCode": "3812", 
    "naicsCode": null 
    }, 
    "tags": [ 
    "Technology", 
    "E-commerce", 
    "Enterprise", 
    "B2B", 
    "B2C", 
    "Consumer Discretionary", 
    "Consumer Electronics" 
    ], 
    "description": "Delivering innovative GPS technology across diverse markets, including aviation, marine, fitness, outdoor recreation, tracking and mobile apps.", 
    "foundedYear": 1989, 
    "location": "Olathe, KS 66062, USA", 
    "timeZone": "America/Chicago", 
    "utcOffset": -5, 
    "geo": { 
    "streetNumber": null, 
    "streetName": null, 
    "subPremise": null, 
    "city": "Olathe", 
    "postalCode": "66062", 
    "state": "Kansas", 
    "stateCode": "KS", 
    "country": "United States", 
    "countryCode": "US", 
    "lat": 38.8271089, 
    "lng": -94.7898731 
    }, 
    "logo": "https://logo.clearbit.com/garmin.com", 
    "facebook": { 
    "handle": "garmin", 
    "likes": 1542748 
    }, 
    "linkedin": { 
    "handle": "company/garmin-international" 
    }, 
    "twitter": { 
    "handle": "Garmin", 
    "id": "15324722", 
    "followers": 136976, 
    "following": 812, 
    "location": "at Garmin HQ just outside KC", 
    }, 
    "crunchbase": { 
    "handle": "organization/garmin" 
    }, 
    "emailProvider": false, 
    "type": "public", 
    "ticker": "GRMN", 
    "phone": "+41 52 630 16 00", 
    "metrics": { 
    "alexaUsRank": 1094, 
    "alexaGlobalRank": 1535, 
    "googleRank": null, 
    "employees": 10000, 
    "employeesRange": "1000+", 
    "marketCap": 9800000000, 
    "raised": null, 
    "annualRevenue": 3018665000, 
    "fiscalYearEnd": 12 
    }, 
    "indexedAt": "2017-07-06T02:54:05.626Z", 
    "tech": [ 
    "centos", 
    "akamai_dns", 
    "apache", 
    "outlook", 
    "microsoft_office_365", 
    "google_analytics", 
    "microsoft_exchange_online", 
    "debian", 
    "tealium", 
    "youtube", 
    "recaptcha" 
    ], 
    "similarDomains": [ 
    "cerner.com", 
    "delorme.com", 
    "fitbit.com", 
    "google.com", 
    "gpscity.com", 
    "lowrance.com", 
    "magellangps.com", 
    "novatel.com", 
    "polar.com", 
    "suunto.com", 
    "thegpsstore.com", 
    "trimble.com" 
    ] 
} 

圖片例如:

enter image description here

所以我現在需要做的僅僅是採取一些數據出來,如「公司名稱「,」類別「(包含所有子類別信息),」員工「,」員工範圍「,」市場營銷「,」年度收入「和」類似域名「(本文應爲全文欄目)在它自己的專欄中。我會很高興能得到任何幫助或指導。

+0

您正在使用什麼版本的SQL Server從您的JSON列中的所有數據? – Siyual

+0

17 ofc :)並且OPENJSON命令請求兼容性級別爲130! – Vissow

+0

https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server –

回答

1

使用JSON_VALUE功能可以提取

SELECT * , 
    JSON_VALUE(JsonColumn,'$.name') AS CompanyName 
    , JSON_VALUE(JsonColumn,'$.category.sector') AS CategorySector 
    , JSON_VALUE(JsonColumn, '$.category.industryGroup') AS CategoryIndustryGroup 
    -- etc 
    FROM YourTable 
+0

立即嘗試。 JsonColumn是在我的情況下稱爲「JSON」的列還是保留JsonColumn的權利? JSON_VALUE(>>>> JsonColumn <<<<,'$ .cat .... – Vissow

+0

將JsonColumn替換爲您存儲JSON的列的名稱 –

+0

謝謝,會做:) – Vissow