将json值存储在变量中

时间:2015-09-08 13:43:34

标签: json postgresql

我有一个postgres函数,输入参数是json。我需要从这个JSON中提取值并将其插入到不同的表中。

管理员将从前端创建表单,部分,步骤和问题,这些数据将作为JSON获取,我需要相应地将数据插入表中。这里每个表单可以有n个部分,ecah部分可以有n个步骤,每个步骤有n个这样的问题。我的想法是将值从JSON提取到变量中,并使用此变量插入表中。我对postgres很新,所以不要知道这是不是一个好主意。

请找到我将要获得的JSOn

{  
   "functionId":"",
   "subFunctionId":"",
   "groupId":"Audit",
   "formId":"",
   "formName":"sample form",
   "submittedBy":"2000269",
   "createdDate":"2015-08-06",
   "updatedBy":"",
   "updatedDate":"",
   "comments":"",
   "formStatusId":"",
   "formStatus":"",
   "formLanguage":"",
   "isFormConfigured":"",
   "formChange":"Yes",
   "sectionLevelChange":"Yes",
   "isActive":"",
   "formVersionNo":"",
   "formFooterDetails":"",
   "formHeaderDetails":"",
   "images":[  
      {  
         "imageId":"",
         "imageTempId":"",
         "imageTempUrl":"",
         "imageName":"",
         "imageUrl":"",
         "isDeleted":"",
         "imagesDesc":""
      }
   ],
   "imagesDescLevel":"",
   "sectionElements":[  
      {  
         "sectionElement":[  
            {  
               "sectionId":"",
               "sectionTempId":"sectionId+DDMMHHSSSS",
               "sectionName":"section1",
               "sectionChange":"Yes",
               "stepLevelChange":"Yes",
               "sectionLabel":"",
               "sectionOrder":"",
               "outOfScopeSection":"false",
               "punchListSection":"false",
               "images":[  
                  {  
                     "imageId":"",
                     "imageTempId":"",
                     "imageTempUrl":"",
                     "imageName":"",
                     "imageUrl":"",
                     "isDeleted":"",
                     "imagesDesc":""
                  }
               ],
               "imagesDescLevel":"",
               "isDeleted":"",
               "stepElements":[  
                  {  
                     "stepElement":[  
                        {  
                           "stepId":"",
                           "stepTempId":"stepId+DDMMHHSSSS",
                           "stepName":"step1",
                           "stepLabel":"",
                           "stepOrder":"",
                           "stepChange":"Yes",
                           "questionLevelChange":"Yes",
                           "images":[  
                              {  
                                 "imageId":"",
                                 "imageTempId":"",
                                 "imageTempUrl":"",
                                 "imageName":"",
                                 "imageUrl":"",
                                 "isDeleted":"",
                                 "imagesDesc":""
                              }
                           ],
                           "imagesDescLevel":"",
                           "isDeleted":"",
                           "questionAnswerElements":[  
                              {  
                                 "questionAnswerElement":[  
                                    {  
                                       "questionId":"",
                                       "questionClientUid":"",
                                       "questionDescription":"",
                                       "questionAccessibility":"",
                                       "isPunchListQuestion":"",
                                       "questionChange":"Yes",
                                       "questionOrder":"",
                                       "isDeleted":"",
                                       "images":[  
                                          {  
                                             "imageId":"",
                                             "imageTempId":"",
                                             "imageTempUrl":"",
                                             "imageName":"",
                                             "imageUrl":"",
                                             "isDeleted":"",
                                             "imagesDesc":""
                                          }
                                       ],
                                       "imagesDescLevel":"",
                                       "answerId":"",
                                       "answerClientUid":"",
                                       "elements":[  
                                          {  
                                             "element":[  
                                                {  
                                                   "elementId":"",
                                                   "elementMapId":"",
                                                   "clientUid":"",
                                                   "clientClass":"",
                                                   "imageTempId":"",
                                                   "imageTempUrl":"",
                                                   "elementType":"Question",
                                                   "elementOrder":"",
                                                   "elementArributuesProp":[  
                                                      {  
                                                         "attributeId":"",
                                                         "attributeName":"",
                                                         "defaultValue":""
                                                      }
                                                   ],
                                                   "elementArributuesVal":[  
                                                      {  
                                                         "value1":"item1"
                                                      }
                                                   ],
                                                   "rule":[  
                                                      {  
                                                         "ruleId":"1",
                                                         "ruleName":"Mandatory",
                                                         "formula":"i>a",
                                                         "formulaData":"i>50",
                                                         "isDeleted":"",
                                                         "addAction":[  
                                                            {  
                                                               "actionId":"1",
                                                               "actionDescription":"",
                                                               "actionTag":"",
                                                               "actionOutput":"",
                                                               "actionOutputValue":"",
                                                               "numberOfFields":"",
                                                               "isDeleted":""
                                                            }
                                                         ]
                                                      }
                                                   ]
                                                }
                                             ]
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

如何从JSON中将值提取到变量中。任何帮助都是明确指出的

由于

2 个答案:

答案 0 :(得分:0)

我可以通过以下查询获取详细信息

select 
     line_item4->'questionDescription',line_item6->'elementMapId',line_item7->'attributeId',line_item8->'value1',line_item9->'ruleId',line_item10->'actionId'
     from tbl d,
     json_array_elements(d.j->'sectionElements') l,
     json_array_elements(l->'sectionElement') AS line_item,
     json_array_elements(line_item->'stepElements') AS line_item1,
     json_array_elements(line_item1->'stepElement') AS line_item2,
     json_array_elements(line_item2->'questionAnswerElements') AS line_item3,
     json_array_elements(line_item3->'questionAnswerElement') AS line_item4,
     json_array_elements(line_item4->'elements') AS line_item5,
     json_array_elements(line_item5->'element') AS line_item6,
     json_array_elements(line_item6->'elementArributuesProp') AS line_item7,
     json_array_elements(line_item6->'elementArributuesVal') AS line_item8,
     json_array_elements(line_item6->'rule') AS line_item9,
     json_array_elements(line_item9->'addAction') AS line_item10

由于

答案 1 :(得分:0)

您可以使用Postgres函数获取值,但是您应该知道如何使用它们-例如,下面,我根据表的特定ID从上下文列中获取了值-

    select id,context,(SELECT *
FROM json_array_elements((tr.context#>'{path}')::json) as obj
WHERE  obj->>'type' = 'Hospital' )->>'displayName' as each_attribute from task_results tr where tr.id = '11e99de6-f2c6-66e8-b45f-1ff041905861'

上下文列中有此数据-

{
"use": "",
"path": [
    {
        "id": "11e837f8-42f2-1c62-8cb2-f703eb1e89ad",
        "type": "Root",
        "displayName": "Virtual Hospital"
    },
    {
        "id": "11e837fb-7c5e-57e2-91ac-ab2e1b35a7e9",
        "type": "Hospital",
        "displayName": "Test hospital"
    },
    {
        "id": "11e8d3c4-e68a-0868-b88f-07054484efea",
        "type": "Room",
        "displayName": "vivek's room"
    }
],
"compoundId": "05a34eccd38956e6b7dc1ba85e5c04c29ef3b9cd5f15a73e448222780d4671c86"

}

因此从上面的json中,我只需要存储在displayName中的医院名称,而“类型”应仅是医院。 在上面的帮助下。