简体   繁体   中英

PostgreSQL query to extract JSON Data

I have the json data in the column codemap from which the codes (belonging to removed, revised or added need to be extracted) from the different coding parameter categories such as cpt drg, modifiers, secondaryDiagnosis, principalDiagnosis, secondaryProcedure, observationhours , dischargeStatus ,principalprocedure,hcpcs) need to be extracted

Some images of how my input and expected data look like are below:

Below is how my JSON data looks like

{
  "cpt": {
    "added": [],
    "correct": [
      {
        "code": "99497",
        "description": "ADVANCE CARE PLANNING FIRST 30 MIN"
      },
      {
        "code": "96160",
        "description": "ADMINISTRATION OF PATIENT-FOCUSED HEALTH RISK ASSESSMENT INSTRUMENT (EG, HEALTH HAZARD APPRAISAL) WITH SCORING AND DOCUMENTATION, PER STANDARDIZED INSTRUMENT"
      }
    ],
    "removed": [],
    "revised": []
  },
  "drg": {
    "correct": [],
    "revised": []
  },
  "poa": {
    "correct": [],
    "revised": []
  },
  "hcpcs": {
    "added": [
      {
        "code": "G0402",
        "description": "INITIAL PREVENTIVE EXAM"
      }
    ],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "modifiers": {
    "added": [
      {
        "code": "25",
        "description": "Significant Separately Identifiable E&M Service by the Same Physician on the Same Day"
      }
    ],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "dischargeStatus": {
    "correct": [],
    "revised": []
  },
  "observationHours": {
    "correct": [],
    "revised": []
  },
  "injectionInfusion": {
    "added": [],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "principalDiagnosis": {
    "correct": [
      {
        "code": "Z0000",
        "description": "ENCNTR FOR GENERAL ADULT MEDICAL EXAM W/O ABNORMAL FINDINGS (ICD-10) "
      }
    ],
    "revised": []
  },
  "principalProcedure": {
    "added": [],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "secondaryDiagnosis": {
    "added": [
      {
        "code": "E1165",
        "description": "TYPE 2 DIABETES MELLITUS WITH HYPERGLYCEMIA (ICD-10) "
      },
      {
        "code": "Z136",
        "description": "ENCOUNTER FOR SCREENING FOR CARDIOVASCULAR DISORDERS (ICD-10) "
      },
      {
        "code": "Z1211",
        "description": "ENCOUNTER FOR SCREENING FOR MALIGNANT NEOPLASM OF COLON (ICD-10) "
      }
    ],
    "correct": [
      {
        "code": "M25512",
        "description": "PAIN IN LEFT SHOULDER (ICD-10) "
      },
      {
        "code": "Z7984",
        "description": "LONG TERM (CURRENT) USE OF ORAL HYPOGLYCEMIC DRUGS (ICD-10) "
      },
      {
        "code": "Z791",
        "description": "LONG TERM (CURRENT) USE OF NON-STEROIDAL NON-INFLAM (NSAID) (ICD-10) "
      },
      {
        "code": "Z0000",
        "description": "ENCNTR FOR GENERAL ADULT MEDICAL EXAM W/O ABNORMAL FINDINGS (ICD-10) "
      },
      {
        "code": "M25512",
        "description": "PAIN IN LEFT SHOULDER (ICD-10) "
      },
      {
        "code": "Z7984",
        "description": "LONG TERM (CURRENT) USE OF ORAL HYPOGLYCEMIC DRUGS (ICD-10) "
      },
      {
        "code": "Z791",
        "description": "LONG TERM (CURRENT) USE OF NON-STEROIDAL NON-INFLAM (NSAID) (ICD-10) "
      },
      {
        "code": "Z1339",
        "description": "ENCNTR SCREEN EXAM FOR OTHER MENTAL HLTH AND BEHAVRL DISORD (ICD-10) "
      }
    ],
    "removed": [],
    "revised": [
      {
        "to": {
          "code": "M25512",
          "description": "PAIN IN LEFT SHOULDER (ICD-10) "
        },
        "from": {
          "code": "Z79899",
          "description": "OTHER LONG TERM (CURRENT) DRUG THERAPY (ICD-10) "
        }
      }
    ]
  },
  "secondaryProcedure": {
    "added": [],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "facilityEvaluationManagement": {
    "added": [],
    "correct": [],
    "removed": [],
    "revised": []
  },
  "professionalEvaluationManagement": {
    "added": [],
    "correct": [],
    "removed": [],
    "revised": [
      {
        "to": {
          "code": "99213",
          "description": "OFF/OP VIS, EST PT, 2KEY COMP: EXPAND PROB HX; EXPAN"
        },
        "from": {
          "code": "99396",
          "description": "PERI COMP PREV MED E&M W/HX/EXAM, EST PT;40-64YR"
        }
      }
    ]
  }
}

Also below is what my input and output tables looks like.

108565-input-data-1.jpg 108546-input-data-2.jpg 108547-output.jpg

Input data:

输入数据

Expected output:

预期输出

My try on the code:

SELECT JSONB_ARRAY_ELEMENTS(codemap->'secondardiagnosis' ->'added' ->> 'code') 
    AS secondarydiagnosisaddedcodeadded, * 
  FROM qa_score_report

I am not getting any output. What can I try to resolve this?

You were providing a string ( code ) to an array function. First parse the array added and with the result set parse code :

SELECT jsonb_array_elements(codemap->'secondaryDiagnosis'->'added')->'code' 
FROM qa_score_report;

Aggregating code s into a single row

SELECT array_to_string(array_agg(trim('"' FROM j::text)),',') 
FROM (SELECT jsonb_array_elements(codemap->'secondaryDiagnosis'->'added')->'code'
      FROM qa_score_report) i (j);

Demo: db<>fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM