如何使用正则表达式将我的json解析为CSV?

时间:2017-10-18 22:29:42

标签: sql regex oracle parsing

我试图通过正则表达式将特定字符串解析为我的oracle表中的列,并且我对在线的不同示例感到非常困惑。

这是我要解析的示例字符串(此字符串是表raw string中名为raw_string_table的列中的单个值):

Attributes : {}
Body : {
    "Type" : "TypeValue",
    "MessageId" : "MessageIDValue",
    "TopicArn" : "TopicArnValue",
    "Message" : "MessageValue",
    "Timestamp" : "TimestampValue",
    "SignatureVersion" : "SignatureVersionValue",
    "Signature" : "SignatureValue",
    "SigningCertURL" : "SigningCertURLValue",
    "UnsubscribeURL" : "UnsubscribeURLValue"
}
MD5OfBody : MD5OfBodyValue

(为了便于阅读而添加了缩进)

这就是我想要的结果集:

example result set

1 个答案:

答案 0 :(得分:0)

最简单的方法是为您尝试提取的每个值执行正则表达式 - 这样您就不依赖于排序:

SELECT REGEXP_SUBSTR(RAW_STRING, '"Type".*:.*"(.*)"', 1, 1, NULL, 1) AS TYPE, 
       REGEXP_SUBSTR(RAW_STRING, '"MessageId".*:.*"(.*)"', 1, 1, NULL, 1) AS MESSAGEID,
       REGEXP_SUBSTR(RAW_STRING, '"TopicArn".*:.*"(.*)"', 1, 1, NULL, 1) AS TOPICARN,
       REGEXP_SUBSTR(RAW_STRING, '"Message".*:.*"(.*)"', 1, 1, NULL, 1) AS MESSAGE,
       REGEXP_SUBSTR(RAW_STRING, '"Timestamp".*:.*"(.*)"', 1, 1, NULL, 1) AS TIMESTAMP,
       REGEXP_SUBSTR(RAW_STRING, '"SignatureVersion".*:.*"(.*)"', 1, 1, NULL, 1) AS SIGNATUREVERSION,
       REGEXP_SUBSTR(RAW_STRING, '"Signature".*:.*"(.*)"', 1, 1, NULL, 1) AS SIGNATURE,
       REGEXP_SUBSTR(RAW_STRING, '"SigningCertURL".*:.*"(.*)"', 1, 1, NULL, 1) AS SIGNINGCERTURL,
       REGEXP_SUBSTR(RAW_STRING, '"UnsubscribeURL".*:.*"(.*)"', 1, 1, NULL, 1) AS UNSUBSCRIBEURL
  FROM RAW_STRING_TABLE;

SQLFiddle here

祝你好运。