Help with powerquery to extract JSON parameter values from the data

Hellothere8028 821 Reputation points
2021-06-23T09:30:01.73+00:00

Hi All,

Hope you are doing well!...I am trying to split the different parameter values for just the correct ,added and removed codes at a vbillid level..In the attached excel file the column codemap has the json 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

Attached the excel file with the input and the output data sheets...Can you please help me here...

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 the way my Input and output tables looks like

108565-input-data-1.jpg108546-input-data-2.jpg108547-output.jpg

Can you please help me here!..

Thanks,
Arun

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2021-06-23T16:07:00.627+00:00

    Here is the code example to parse your JSON:

    DECLARE @json nvarchar(max) = N'
    {
       "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"
                }
             }
          ]
       }
    }
    ';
    
    DECLARE @Categories TABLE (
        RowId int IDENTITY(1, 1),
        Category nvarchar(100),
        ValueInJSON nvarchar(max),
        ObjectType smallint
    );
    
    INSERT INTO @Categories
    SELECT *
    FROM OPENJSON(@json);
    
    DECLARE @rowID int = 1;
    DECLARE @maxRowID int = (SELECT MAX(RowID) FROM @Categories);
    DECLARE @category nvarchar(100);
    -- Output table
    DECLARE @OutputTable TABLE (
        ErrorParameter nvarchar(100),
        Type_Of_Correction nvarchar(20),
        Coded_Value nvarchar(20),
        New_Value nvarchar(20)
    );
    
    WHILE (@rowID <= @maxRowID)
    BEGIN
        SELECT @category = Category FROM @Categories WHERE RowID = @rowID;
    
        INSERT INTO @OutputTable(ErrorParameter, Type_Of_Correction, Coded_Value, New_Value)
        SELECT @category AS ErrorParametr, 'added' AS Type_Of_Correction, '' AS Coded_Value, code AS New_Value
        FROM OPENJSON(@json, '$.' + @category + '.added')
        WITH (
            code varchar(10) '$.code'
        )
        UNION
        SELECT @category AS ErrorParametr, 'correct' AS Type_Of_Correction, '' AS Coded_Value, code AS New_Value
        FROM OPENJSON(@json, '$.' + @category + '.correct')
        WITH (
            code varchar(10) '$.code'
        )
        UNION
        SELECT @category AS ErrorParametr, 'removed' AS Type_Of_Correction, '' AS Coded_Value, code AS New_Value
        FROM OPENJSON(@json, '$.' + @category + '.removed')
        WITH (
            code varchar(10) '$.code'
        )
        UNION
        SELECT @category AS ErrorParametr, 'revised' AS Type_Of_Correction, from_code AS Coded_Value, to_code AS New_Value
        FROM OPENJSON(@json, '$.' + @category + '.revised')
        WITH (
            from_code varchar(10) '$.from.code',
            to_code varchar(10) '$.to.code'
        );
    
        SET @rowID = @rowID + 1;
    END
    
    SELECT * FROM @OutputTable;
    

4 additional answers

Sort by: Most helpful
  1. Hellothere8028 821 Reputation points
    2021-06-24T07:37:48.507+00:00

    @Guoxiong : Really appreciate your help!..But I am also looking for some help with powerquery for the above json data..

    0 comments No comments

  2. Herbert Seidenberg 1,191 Reputation points
    2021-06-25T00:42:23.417+00:00

    Excel 365 Pro Plus with Power Pivot and Power Query.
    With EasyDataTransform for json.
    https://www.mediafire.com/file/88e80bm42naw070/06_23_21.xlsx/file
    https://www.mediafire.com/file/ig9iunz3kqkwiv0/06_23_21.pdf/file


  3. Lz._ 9,016 Reputation points
    2021-07-19T12:32:59.053+00:00

    @Hellothere8028

    The Json representation you posted as text is parsed with Power Query in this file to download


  4. Lz._ 9,016 Reputation points
    2021-07-19T14:05:13.653+00:00

    @Hellothere8028

    Just uploaded a better version here

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.