0

This works in SQL Server:

    declare @jstring varchar(max)='{
    "FieldInfo": {
        "Template": "Babies",
        "Groups": [{
            "Group": "Required",
            "SortOrder": 1,
            "BackgroundColor": "FCD5B4",
            "Fields": [{
                "fieldName": "feed_product_type",
                "SortOrder": 1,
                "labelName": "Product Type",
                "defaultValue": "",
                "columnIndex": 0
            }, {
                "fieldName": "item_sku",
                "SortOrder": 2,
                "labelName": "Seller SKU",
                "defaultValue": "",
                "columnIndex": 1
            }, {
                "fieldName": "brand_name",
                "SortOrder": 3,
                "labelName": "Brand",
                "defaultValue": "",
                "columnIndex": 2
            }, {
                "fieldName": "item_name",
                "SortOrder": 4,
                "labelName": "Title",
                "defaultValue": "",
                "columnIndex": 3
            }, {
                "fieldName": "external_product_id",
                "SortOrder": 5,
                "labelName": "Product ID",
                "defaultValue": "",
                "columnIndex": 4
            }, {
                "fieldName": "external_product_id_type",
                "SortOrder": 6,
                "labelName": "Product ID Type",
                "defaultValue": "",
                "columnIndex": 5
            }, {
                "fieldName": "item_type",
                "SortOrder": 7,
                "labelName": "Item Type Keyword",
                "defaultValue": "",
                "columnIndex": 6
            }, {
                "fieldName": "model",
                "SortOrder": 8,
                "labelName": "Model Number",
                "defaultValue": "",
                "columnIndex": 7
            }, {
                "fieldName": "manufacturer",
                "SortOrder": 9,
                "labelName": "Manufacturer",
                "defaultValue": "",
                "columnIndex": 8
            }, {
                "fieldName": "part_number",
                "SortOrder": 10,
                "labelName": "Part Number",
                "defaultValue": "",
                "columnIndex": 9
            }, {
                "fieldName": "mfg_minimum",
                "SortOrder": 11,
                "labelName": "Minimum Manufacturer Age  Recommended",
                "defaultValue": "",
                "columnIndex": 10
            }, {
                "fieldName": "standard_price",
                "SortOrder": 12,
                "labelName": "Standard Price",
                "defaultValue": "",
                "columnIndex": 11
            }, {
                "fieldName": "quantity",
                "SortOrder": 13,
                "labelName": "Quantity",
                "defaultValue": "",
                "columnIndex": 12
            }, {
                "fieldName": "main_image_url",
                "SortOrder": 14,
                "labelName": "Main Image URL",
                "defaultValue": "",
                "columnIndex": 13
            }]
        }, {
            "Group": "Images",
            "SortOrder": 2,
            "BackgroundColor": "FFFF00",
            "Fields": [{
                "fieldName": "other_image_url1",
                "SortOrder": 15,
                "labelName": "Other Image URL1",
                "defaultValue": "",
                "columnIndex": 14
            }, {
                "fieldName": "other_image_url2",
                "SortOrder": 16,
                "labelName": "Other Image URL2",
                "defaultValue": "",
                "columnIndex": 15
            }, {
                "fieldName": "other_image_url3",
                "SortOrder": 17,
                "labelName": "Other Image URL3",
                "defaultValue": "",
                "columnIndex": 16
            }, {
                "fieldName": "other_image_url4",
                "SortOrder": 18,
                "labelName": "Other Image URL4",
                "defaultValue": "",
                "columnIndex": 17
            }, {
                "fieldName": "other_image_url5",
                "SortOrder": 19,
                "labelName": "Other Image URL5",
                "defaultValue": "",
                "columnIndex": 18
            }, {
                "fieldName": "other_image_url6",
                "SortOrder": 20,
                "labelName": "Other Image URL6",
                "defaultValue": "",
                "columnIndex": 19
            }, {
                "fieldName": "other_image_url7",
                "SortOrder": 21,
                "labelName": "Other Image URL7",
                "defaultValue": "",
                "columnIndex": 20
            }, {
                "fieldName": "other_image_url8",
                "SortOrder": 22,
                "labelName": "Other Image URL8",
                "defaultValue": "",
                "columnIndex": 21
            }, {
                "fieldName": "swatch_image_url",
                "SortOrder": 23,
                "labelName": "Swatch Image URL",
                "defaultValue": "",
                "columnIndex": 22
            }]
        }, {
            "Group": "Variation",
            "SortOrder": 3,
            "BackgroundColor": "FF8080",
            "Fields": [{
                "fieldName": "parent_child",
                "SortOrder": 24,
                "labelName": "Parentage",
                "defaultValue": "",
                "columnIndex": 23
            }, {
                "fieldName": "relationship_type",
                "SortOrder": 25,
                "labelName": "Relationship Type",
                "defaultValue": "",
                "columnIndex": 24
            }, {
                "fieldName": "parent_sku",
                "SortOrder": 26,
                "labelName": "Parent SKU",
                "defaultValue": "",
                "columnIndex": 25
            }, {
                "fieldName": "variation_theme",
                "SortOrder": 27,
                "labelName": "Variation Theme",
                "defaultValue": "",
                "columnIndex": 26
            }]
        }, {
            "Group": "Basic",
            "SortOrder": 4,
            "BackgroundColor": "F8A45E",
            "Fields": [{
                "fieldName": "update_delete",
                "SortOrder": 28,
                "labelName": "Update Delete",
                "defaultValue": "",
                "columnIndex": 27
            }, {
                "fieldName": "product_description",
                "SortOrder": 29,
                "labelName": "Description",
                "defaultValue": "",
                "columnIndex": 28
            }, {
                "fieldName": "care_instructions",
                "SortOrder": 30,
                "labelName": "Care Instructions",
                "defaultValue": "",
                "columnIndex": 29
            }, {
                "fieldName": "target_gender",
                "SortOrder": 31,
                "labelName": "Target Gender",
                "defaultValue": "",
                "columnIndex": 30
            }, {
                "fieldName": "edition",
                "SortOrder": 32,
                "labelName": "Edition",
                "defaultValue": "",
                "columnIndex": 31
            }]
        }]
    }
}'
--drop table #AmazonTemplateInfo
;with FieldInfo as (
select Template,[Group],GroupSort,BackgroundColor,FieldName,FieldSort,LabelName,DefaultValue,ColumnIndex from OPENJSON(@jstring,'$.FieldInfo')
        With (
            Template varchar(50)
            ,Groups nvarchar(max) as json
        ) as Template
    cross apply openjson (Template.Groups)
        with (
            [Group] varchar(50)
            ,BackgroundColor varchar(6)
            ,GroupSort int '$.SortOrder'
            ,Fields nvarchar(max) as json
        ) Groups
    cross apply openjson(Groups.Fields)
        with (
            fieldName varchar(50)
            ,FieldSort int '$.SortOrder'
            ,labelName varchar(50)
            ,defaultValue varchar(max)
            ,columnIndex int
        ) Fields
)
select * from FieldInfo

This generates a table in the form

Template Group GroupSort BackgroundColor FieldName FieldSort LabelName DefaultValue ColumnIndex
Babies Required 1 FCD5B4 feed_product_type 1 Product Type 0

I need to create the same functionality in MariaDB v10. I've tried using JSON_EXTRACT and the LATERAL statement for my cross applies, but nothing is working. JSON_EXTRACT seems to just create a JSON string from the selected columns.

Is this pointless in MariaDB? Is there any way to do this?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Chris
  • 650
  • 7
  • 20
  • There is no version 10 for MySQL, so I assume you must be using MariaDB. I have edited your question and tags to make it clear you are using MariaDB. – Bill Karwin Jan 20 '21 at 21:16
  • Yes...that is correct. I was just handed this project and I did not even notice that. – Chris Jan 20 '21 at 21:23

2 Answers2

0

MySQL has a feature that is close to what you're describing, it's the JSON_TABLE() function. See documentation and examples:

However, you said you're using "v10" which is a MariaDB version number, not a MySQL version number. MariaDB and MySQL are different software products. They had a common origin of source code in 2010, but they have been slowly diverging since then. In that way, it's a bit like comparing Microsoft SQL Server to Sybase.

No version of MariaDB at least up to 10.5 implements the JSON_TABLE() function. Perhaps some future version will.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

Well...finally. In case anyone is interested, this is how I parsed a multi-level JSON string in MariaDB:

set @jstring='{"FieldInfo": {"Template": "Babies","Groups": [{"Group": "Required","SortOrder": 1,"BackgroundColor": "FCD5B4","Fields": [{"fieldName": "feed_product_type","SortOrder": 1,"labelName": "Product Type","defaultValue": "","columnIndex": 0}, {"fieldName": "item_sku","SortOrder": 2,"labelName": "Seller SKU","defaultValue": "","columnIndex": 1}, {"fieldName": "brand_name","SortOrder": 3,"labelName": "Brand","defaultValue": "","columnIndex": 2}, {"fieldName": "item_name","SortOrder": 4,"labelName": "Title","defaultValue": "","columnIndex": 3}, {"fieldName": "external_product_id","SortOrder": 5,"labelName": "Product ID","defaultValue": "","columnIndex": 4}, {"fieldName": "external_product_id_type","SortOrder": 6,"labelName": "Product ID Type","defaultValue": "","columnIndex": 5}, {"fieldName": "item_type","SortOrder": 7,"labelName": "Item Type Keyword","defaultValue": "","columnIndex": 6}, {"fieldName": "model","SortOrder": 8,"labelName": "Model Number","defaultValue": "","columnIndex": 7}, {"fieldName": "manufacturer","SortOrder": 9,"labelName": "Manufacturer","defaultValue": "","columnIndex": 8}, {"fieldName": "part_number","SortOrder": 10,"labelName": "Part Number","defaultValue": "","columnIndex": 9}, {"fieldName": "mfg_minimum","SortOrder": 11,"labelName": "Minimum Manufacturer Age  Recommended","defaultValue": "","columnIndex": 10}, {"fieldName": "standard_price","SortOrder": 12,"labelName": "Standard Price","defaultValue": "","columnIndex": 11}, {"fieldName": "quantity","SortOrder": 13,"labelName": "Quantity","defaultValue": "","columnIndex": 12}, {"fieldName": "main_image_url","SortOrder": 14,"labelName": "Main Image URL","defaultValue": "","columnIndex": 13}]}, {"Group": "Images","SortOrder": 2,"BackgroundColor": "FFFF00","Fields": [{"fieldName": "other_image_url1","SortOrder": 15,"labelName": "Other Image URL1","defaultValue": "","columnIndex": 14}, {"fieldName": "other_image_url2","SortOrder": 16,"labelName": "Other Image URL2","defaultValue": "","columnIndex": 15}, {"fieldName": "other_image_url3","SortOrder": 17,"labelName": "Other Image URL3","defaultValue": "","columnIndex": 16}, {"fieldName": "other_image_url4","SortOrder": 18,"labelName": "Other Image URL4","defaultValue": "","columnIndex": 17}, {"fieldName": "other_image_url5","SortOrder": 19,"labelName": "Other Image URL5","defaultValue": "","columnIndex": 18}, {"fieldName": "other_image_url6","SortOrder": 20,"labelName": "Other Image URL6","defaultValue": "","columnIndex": 19}, {"fieldName": "other_image_url7","SortOrder": 21,"labelName": "Other Image URL7","defaultValue": "","columnIndex": 20}, {"fieldName": "other_image_url8","SortOrder": 22,"labelName": "Other Image URL8","defaultValue": "","columnIndex": 21}, {"fieldName": "swatch_image_url","SortOrder": 23,"labelName": "Swatch Image URL","defaultValue": "","columnIndex": 22}]}, {"Group": "Variation","SortOrder": 3,"BackgroundColor": "FF8080","Fields": [{"fieldName": "parent_child","SortOrder": 24,"labelName": "Parentage","defaultValue": "","columnIndex": 23}, {"fieldName": "relationship_type","SortOrder": 25,"labelName": "Relationship Type","defaultValue": "","columnIndex": 24}, {"fieldName": "parent_sku","SortOrder": 26,"labelName": "Parent SKU","defaultValue": "","columnIndex": 25}, {"fieldName": "variation_theme","SortOrder": 27,"labelName": "Variation Theme","defaultValue": "","columnIndex": 26}]}, {"Group": "Basic","SortOrder": 4,"BackgroundColor": "F8A45E","Fields": [{"fieldName": "update_delete","SortOrder": 28,"labelName": "Update Delete","defaultValue": "","columnIndex": 27}, {"fieldName": "product_description","SortOrder": 29,"labelName": "Description","defaultValue": "","columnIndex": 28}, {"fieldName": "care_instructions","SortOrder": 30,"labelName": "Care Instructions","defaultValue": "","columnIndex": 29}, {"fieldName": "target_gender","SortOrder": 31,"labelName": "Target Gender","defaultValue": "","columnIndex": 30}, {"fieldName": "edition","SortOrder": 32,"labelName": "Edition","defaultValue": "","columnIndex": 31}]}]}}'

;with t1 as (
    select JSON_Value(@jstring,'$.FieldInfo.Template') as Template, JSON_Query(@jstring,'$.FieldInfo.Groups') as jStringGroups
)
SELECT Template,jstringGroups into @Template, @jStringGroups from t1;
set @numGroups=JSON_Length(@jStringGroups,'$');
SET max_recursive_iterations=100000; 


;with recursive  t2  as (
    select 0 curLev
        , @Template Template
        , @jStringGroups jStringGroups
        , JSON_Value(@jStringGroups,'$[0].Group') 'Group'
        ,cast(JSON_Value(@jStringGroups,'$[0].SortOrder') as int) GroupSort
        ,JSON_Value(@jStringGroups,'$[0].BackgroundColor') BackgroundColor
        ,JSON_Query(@jStringGroups,'$[0].Fields') fieldString 
        ,CAST(JSON_Length(JSON_Query(@jStringGroups,'$[0].Fields'),'$') as int) numFields
       UNION
    select t2.curLev+1
        , t2.Template
        , t2.jStringGroups
        , JSON_Value(t2.jStringGroups,CONCAT('$[',t2.curLev+1,'].Group'))
        , JSON_Value(t2.jStringGroups,CONCAT('$[',t2.curLev+1,'].SortOrder')) 
        , JSON_Value(t2.jStringGroups,CONCAT('$[',t2.curLev+1,'].BackgroundColor'))
        , JSON_Query(t2.jStringGroups,CONCAT('$[',t2.curLev+1,'].Fields'))  
        , JSON_Length(JSON_Query(t2.jStringGroups,CONCAT('$[',t2.curLev+1,'].Fields')),'$')
    from t2 where t2.curLev+1<@numGroups
),
t3 as (
        select 0 curLev
            ,Template
            ,t2.Group
            ,GroupSort
            ,BackgroundColor
            ,fieldString
            ,numfields
            ,JSON_Value(t2.fieldString,'$[0].fieldName') fieldName
            ,cast(JSON_Value(t2.fieldString,'$[0].SortOrder') as int) fieldSort
            ,JSON_Value(t2.fieldString,'$[0].labelName') labelName
            ,JSON_Value(t2.fieldString,'$[0].defaultValue') defaultValue
            ,cast(JSON_Value(t2.fieldString,'$[0].columnIndex') as int) columnIndex
        from t2
        UNION
        select t3.curLev+1
            ,t3.Template
            ,t3.Group
            ,t3.GroupSort
            ,t3.BackgroundColor
            ,t3.fieldString
            ,t3.numfields
            ,JSON_Value(t3.fieldString,CONCAT('$[',t3.curLev+1,'].fieldName'))
            ,JSON_Value(t3.fieldString,CONCAT('$[',t3.curLev+1,'].SortOrder'))
            ,JSON_Value(t3.fieldString,CONCAT('$[',t3.curLev+1,'].labelName'))
            ,JSON_Value(t3.fieldString,CONCAT('$[',t3.curLev+1,'].defaultValue'))
            ,JSON_Value(t3.fieldString,CONCAT('$[',t3.curLev+1,'].columnIndex'))
        from t3
        where t3.curLev+1<t3.numFields
    )

select Template,t3.Group,GroupSort,FieldName,FieldSort,LabelName,DefaultValue,ColumnIndex from t3 order by GroupSort, fieldSort;

This got me my table.

Chris
  • 650
  • 7
  • 20