使用JSON_MODIFY在JSON开头添加元素

时间:2019-04-02 14:05:53

标签: json sql-server sql-server-2016

我正在尝试根据下表构建JSON

Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null}

使用查询

 name | flag 
------+------
 foo  | fail 
 bar  | pass 

此处生成的JSON是

DECLARE @JSONDATA nvarchar(MAX) = (SELECT [name], [flag]
FROM test
FOR JSON AUTO, ROOT('students'))
SET @JSONDATA = JSON_MODIFY(@JSONDATA, '$.class','10')

我需要在JSON的第一个节点上使用{ "students": [ { "name": "foo", "flag": "fail" }, { "name": "bar", "flag": "pass" } ], "class": "10" } 元素。有没有办法使用class

Fiddle

1 个答案:

答案 0 :(得分:2)

不知所措,无法通过修改强制执行序列。

也许是另一种选择

def showvideo(request):
    allvideos = Video.objects.all()
    form = VideoForm(request.POST or None)
    if form.is_valid():
        sirina = form.cleaned_data.get('sirina')
        visina = form.cleaned_data.get('visina')
        category = form.cleaned_data.get('category')
        roletne = form.cleaned_data.get('roletne')
        okapnice = form.cleaned_data.get('okapnice')
        materijal = form.cleaned_data.get('materijal')
        komore = form.cleaned_data.get('komore')
        krila = form.cleaned_data.get('krila')

        #Example 
        a = 20 + sirina
        b = 30 + visina 
        c = 50 + roletne
        ## I want to save those variables in database and load them after 
        ## in template



        form.save()

    context={'form':form}
    return render(request,'index.html',context)

返回

Select class=10
      ,students =  (SELECT [name], [flag] FROM test FOR JSON AUTO)
  For JSON path, without_array_wrapper 

EDIT-根据GSerg的建议更新了SELECT