2017-07-27 43 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