Click here to Skip to main content
14,929,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have designed a table to accomodate user defined fields. Basically they all go off a Case Reference such as 'TEST'. All of the fields are stored within a json column inside a json object array. I need to query multiple fields into a JSON_TABLE.

How would i query this to get the fields such as: Fields.test, Fields.test2

The JSON Template:
{"field": [
{"value": "30/07/2020", "FieldName": "Fields.test"},
{"value": "29/07/2020", "FieldName": "Fields.test2"}
]}


What I have tried:

I am able to query for the value of one of the fields using JSON_TABLE like below:
SELECT CaseRef, CaseTypeIndex,name,value FROM FieldValues, JSON_TABLE ( FieldValues.json, "$.field[*]" COLUMNS ( name VARCHAR(100) PATH "$.FieldName", value VARCHAR(100) PATH "$.value")) as Results WHERE name = 'Fields.test'
Posted
Updated 14-Aug-20 2:02am

1 solution

Depending upon what you're doing with the data (most of the time) I'd change from JSON storage to a tag/value format. The tag can be the user's defined field name, the value is (all stored as char, initially) just that. You can then easily gather whatever you wish.

In another column you can create the associative link. A parent table can define each record set and supply that associative link.

Together, you can have any number of user defined value for each parent reference - zero or more - essentially without limit.

As an aside - it was only a few weeks ago that I looked into the JSON field type for SQL and thought it was really neat. Well - my boss is a very good DBA - and he pointed out how (in most circumstances) it would be very inefficient and cause a serious load on system. The ability to internally index the fields he found horrific in performance consequences.


So - picture your data "vertically oriented" as seperate associated records instead of horizontally oriented as JSON text.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900