使用Oracle SQL解析类似文本的JSON

时间:2018-03-12 03:16:35

标签: sql oracle parsing

我要求在Oracle中解析XML文本字段以删除数据中的特定字符/字符串。

I / p: -

{{Value : "Actual: 15' 0" X 7' 0"  Opening:  15' 0" X 7' 0"", Description : "Size", PrintCode : "", PrintSequence : 80}, 
{Value : "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", PrintCode : "", PrintSequence : 90}, 
{Value : "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", PrintCode : "", PrintSequence : 100},
{Value : "Size: 2"-,Mount: Bracket  Mount-,Radius: 15"", Description : "Track", PrintCode : "", PrintSequence : 110},
{Value : "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", PrintCode : "", PrintSequence : 120},
{Value : "Hinge: Standard-,Struts: Standard", Description : "Hardware", PrintCode : "", PrintSequence : 130}}

我需要像O / P一样 -

"Actual: 15' 0" X 7' 0"  Opening:  15' 0" X 7' 0"", Description : "Size",
"Section Color: Desert Tan-,Trim Board Color: White", Description : "Color",
"Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", 
"Size: 2"-,Mount: Bracket  Mount-,Radius: 15"", Description : "Track",
"Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance",
"Hinge: Standard-,Struts: Standard", Description : "Hardware"

1)我想删除所有括号。
2)我想删除所有以PrintCode开头的代码,直到括号结束 3)应将value :字符串替换为null。

非常感谢任何帮助。谢谢。 :)

1 个答案:

答案 0 :(得分:0)

试试这个。

SQL Fiddle

模式匹配大括号 - {} value : PrintCode until the bracket end,并将其替换为空白。删除trims和空格需要额外的,.+?用于非贪婪匹配,搜索直到第一次出现下一个字符(在这种情况下为})。

我希望代码中没有新的行,正如您在评论中所说的那样。结果可能会有所不同,n模式匹配选项可用于处理它。

查询1

WITH t ( input ) AS
  (SELECT '{{Value : "Actual: 15'' 0" X 7'' 0"  Opening:  15'' 0" X 7'' 0"", Description : "Size", PrintCode : "", PrintSequence : 80}, {Value : "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", PrintCode : "", PrintSequence : 90}, {Value : "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", PrintCode : "", PrintSequence : 100},{Value : "Size: 2"-,Mount: Bracket  Mount-,Radius: 15"", Description : "Track", PrintCode : "", PrintSequence : 110},{Value : "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", PrintCode : "", PrintSequence : 120},{Value : "Hinge: Standard-,Struts: Standard", Description : "Hardware", PrintCode : "", PrintSequence : 130}}'
  FROM dual
  )
SELECT RTRIM ( TRIM ( REGEXP_REPLACE (input,'({|}|Value +:|PrintCode.+?}(,|}))', '')) ,',') AS output
FROM t

<强> Results

"Actual: 15' 0" X 7' 0" Opening: 15' 0" X 7' 0"", Description : "Size", "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", "Size: 2"-,Mount: Bracket Mount-,Radius: 15"", Description : "Track", "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", "Hinge: Standard-,Struts: Standard", Description : "Hardware"