sqljson解析
Ⅰ 如何解析JSON數組到sql表
例如:
源JSON數據:
{
"item1":{
"title":"2",
"value":null,
"visible":true,
"name":"item1",
"enabled":true,
"readonly":false,
"id":"f1f46ce6-9d0b-4eaf-88b7-d35b23a4d2e4"
},
"item2":{
"title":null,
"value":null
"visible":true,
"name":"item2",
"enabled":true,
"readonly":false,
"id":"da2b8a02-cfbd-4de8-8a33-74e2a484475a"
},
"item3":{
"title":"",
"value":null,
"visible":true,
"name":"item3",
"enabled":true,
"readonly":false,
"id":"57ee45d6-41d7-45c2-b022-13220e31d2d2"
}}
SQL查詢
SELECT[Key].[key]AS[ItemName],[Value].*FROMOPENJSON(@json,'$')AS[Key]CROSSAPPLYOPENJSON([Key].value)
WITH(
TitleVARCHAR(100)'$.title',
ValueVARCHAR(100)'$.value',
VisibleVARCHAR(100)'$.visible',
NameVARCHAR(100)'$.name',
EnabledVARCHAR(100)'$.enabled',
ReadOnlyVARCHAR(100)'$.readonly',
IdVARCHAR(500)'$.id'
)AS[Value]
Ⅱ SQL server存儲過程實現JSON數據解析,然後插入資料庫表求高手指點
兩種方式
1、SQL有個charindex 函數,可以用這個函數配合substr實現 split功能實現循環插入
2、sql 2008以上存儲過程支持表值參數,json反序列化在程序里更方便,所以反序列化之後通過表值參數傳遞
Ⅲ sql中json解析
你好!
withtas(select'a:[{f:,h:,checindate:''month1:,year:,day:'',checkoutdate:''month:,year:,day:'',},
{checindate:''month2:,year:,day:,'',checkoutdate:''month:,year:,day},
{checindate:''month3:,year:,day:,'',checkoutdate:''month:,year:,day}]'strfromal)
,t1as(SELECTsubstr(str,instr(str,'[')+1,instr(str,']')-instr(str,'[')-1)strFROMT)
,t2as(selectsubstr(str,instr(str,'{')+1,instr(str,'}')-instr(str,'{')-1)strfromt1)
selectstr,substr(str,instr(str,'checindate')+12,instr(str,'checkoutdate')-instr(str,'checindate')-12)fromt2;
得到第一個checindate,直接截取字元串就可以了
別搞得那麼復雜了
Ⅳ sql中對json數據欄位的查詢
先取出string,再在內存里轉換為對象並檢查。
ps:存json是沒問題,但又想存json又想直接查,違反了資料庫的範式。
Ⅳ mysql資料庫中某個欄位存的是json數據,如何對json數據中的數據進行操作
這個可以吧json格式的字元串解析成數組json_decode()函數,變成數組以後就可以方便操作了,可以刪除數組中的任意一項,也可以增加一項比如:array_push($data,['sort'=>3,'catentryId'=>10003]),再變成json格式的存入資料庫。方法有多種,這里簡單的示例下
Ⅵ 如何獲取從his系統json的數據解析為xml插入sql用c#實現
序列化只是將數據序列化為完整的准確無誤的json格式的數據!
解析指的就是將你上面的json數據一一從json格式中分解出來,的到字元串格式的便於封裝bean對像
建立一對多的對象
將value setter給你的bean對象!
最後將bean存資料庫
Ⅶ 剛入職的菜鳥,在plsql中怎麼拼接json欄位
Oracle 12.1.0.2版本有一個新功能就是可以存儲、查詢、索引JSON數據格式,而且也實現了使用SQL語句來解析JSON,非常方便。JSON數據在資料庫中以VARCHAR2, CLOB或者BLOB進行存儲。Oracle建議用戶在插入JSON數據之前,使用is_json來驗證輸入JSON數據的正確性。另外,Oracle也提供了相關的函數:
Functions:json_value, json_query, json_table.
Conditions:json_exists, is json, is not json, json_textcontains.
Ⅷ java將json數據解析為sql語句
importjava.util.Iterator;
importjava.util.Set;
importjava.util.Map.Entry;
importcom.google.gson.JsonArray;
importcom.google.gson.JsonElement;
importcom.google.gson.JsonObject;
importcom.google.gson.JsonParser;
publicclassSql
{
publicstaticStringparseSQL(Stringjson)
{
JsonParserparser=newJsonParser();
JsonObjectobj=(JsonObject)parser.parse(json);;
Stringtable=obj.get("table").getAsString();
Stringop_type=obj.get("op_type").getAsString();
Stringsql="";
if("I".equals(op_type))
{
sql+="INSERTINTO"+table+"(";
JsonObjectafter=(JsonObject)obj.get("after");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
Stringvs="values(";
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+",";
vs+=val+",";
}
sql=sql.replaceAll(",\s*$","");
vs=vs.replaceAll(",\s*$","");
sql+=")"+vs+")";
}
elseif("U".equals(op_type))
{
sql+="UPDATE"+table+"SET";
JsonObjectafter=(JsonObject)obj.get("after");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+",";
}
sql=sql.replaceAll(",\s*$","");
sql+="WHERE";
after=(JsonObject)obj.get("before");
entry=after.entrySet();
it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+"AND";
}
sql=sql.replaceAll("\s*AND\s*$","");
}
elseif("D".equals(op_type))
{
sql+="DELETEFROM"+table+"WHERE";
JsonObjectafter=(JsonObject)obj.get("before");
Set<Entry<String,JsonElement>>entry=after.entrySet();
Iterator<Entry<String,JsonElement>>it=entry.iterator();
while(it.hasNext())
{
Entry<String,JsonElement>elem=it.next();
Stringkey=elem.getKey();
Stringval=elem.getValue().toString();
sql+=key+"="+val+"AND";
}
sql=sql.replaceAll("\s*AND\s*$","");
}
returnsql;
}
publicstaticvoidmain(String[]args)
{
Stringinsert=
"{"table":"GG.TCUSTORD","op_type":"I","op_ts":"2013-06-0222:14:36.000000","current_ts":"2015-09-18T13:39:35.447000","pos":"00000000000000001444","tokens":{"R":"AADPkvAAEAAEqL2AAA"},"after":{"CUST_CODE":"WILL","ORDER_DATE":"1994-09-30:15:33:00","PRODUCT_CODE":"CAR","ORDER_ID":"144","PRODUCT_PRICE":17520.00,"PRODUCT_AMOUNT":3,"TRANSACTION_ID":"100"}}";
Stringupdate=
"{"table":"GG.TCUSTORD","op_type":"U","op_ts":"2013-06-0222:14:41.000000","current_ts":"2015-09-18T13:39:35.748000","pos":"00000000000000002891","tokens":{"L":"206080450","6":"9.0.80330","R":"AADPkvAAEAAEqLzAAC"},"before":{"CUST_CODE":"BILL","ORDER_DATE":"1995-12-31:15:00:00","PRODUCT_CODE":"CAR","ORDER_ID":"765","PRODUCT_PRICE":15000.00,"PRODUCT_AMOUNT":3,"TRANSACTION_ID":"100"},"after":{"CUST_CODE":"BILL","ORDER_DATE":"1995-12-31:15:00:00","PRODUCT_CODE":"CAR","ORDER_ID":"765","PRODUCT_PRICE":14000.00,"PRODUCT_AMOUNT":3,"TRANSCATION_ID":"100"}}";
Stringdelete=
"{"table":"GG.TCUSTORD","op_type":"D","op_ts":"2013-06-0222:14:41.000000","current_ts":"2015-09-18T13:39:35.766000","pos":"00000000000000004338","tokens":{"L":"206080450","6":"9.0.80330","R":"AADPkvAAEAAEqLzAAC"},"before":{"CUST_CODE":"DAVE","ORDER_DATE":"1993-11-03:07:51:35","PRODUCT_CODE":"PLANE","ORDER_ID":"600"}}";
System.out.println(parseSQL(insert));
System.out.println(parseSQL(update));
System.out.println(parseSQL(delete));
}
}