使用R生成嵌套JSON

问题描述 投票:0回答:2

我正在尝试使用R生成嵌套的JSON文件,并且在设置它时遇到了困难。我承认对JSON非常陌生,因此从概念上讲我落后了。我将不胜感激任何帮助-包括在其他地方(R以外)提供的建议,可以使此任务更轻松。

我正在使用3个数据集-全部通过键连接。本质上,我的目标是创建一个包含患者对象的JSON文件,该对象具有嵌套的遭遇和嵌套的索赔行。

这是R中的设置:

patients <- data.frame (
                      PatientID = c('PID01','PID02'),
                      PatientName = c('John Doe','Jane Doe'),
                      PatientGroup = c('Group A','Group B')
                    )

encounters <- data.frame (
                          EncounterID = c('Enc01','Enc02','Enc03','Enc04','Enc05'),
                          PatientID = c('PID01','PID01','PID02','PID02','PID02'),
                          EncounterType = c('Outpatient','Outpatient','Inpatient','Outpatient','SNF')
                         )

encounterLines <- data.frame (
                              EncounterID = c(rep('Enc01',5),rep('Enc04',2)),
                              RevCodes = c('001','100','200','300','400','001','100'),
                              ClaimLine = c(seq(1:5),seq(1:2))
                              )

 ----------------------------------------
 PatientID   PatientName   PatientGroup 
----------- ------------- --------------
   PID01      John Doe       Group A    

   PID02      Jane Doe       Group B    
----------------------------------------

-----------------------------------------
 EncounterID   PatientID   EncounterType 
------------- ----------- ---------------
    Enc01        PID01      Outpatient   

    Enc02        PID01      Outpatient   

    Enc03        PID02       Inpatient   

    Enc04        PID02      Outpatient   

    Enc05        PID02          SNF      
-----------------------------------------

------------------------------------
 EncounterID   RevCodes   ClaimLine 
------------- ---------- -----------
    Enc01        001          1     

    Enc01        100          2     

    Enc01        200          3     

    Enc01        300          4     

    Enc01        400          5     

    Enc04        001          1     

    Enc04        100          2     
------------------------------------

我正在寻找的JSON输出如下。我尝试使用jsonlite,但似乎无法获得任何实际的牵引力:

[
    {
        "PatientID": "PID01",
        "PatientName": "John Doe",
        "PatientGroup": "Group A",
        "Encounters": [
            {
                "EncounterID": "Enc01",
                "EncounterType": "Outpatient",
                "ClaimLines": [
                    {
                        "ClaimLine": 1,
                        "RevenueCode": "001"
                    },
                    {
                        "ClaimLine": 2,
                        "RevenueCode": "100"
                    },
                    {
                        "ClaimLine": 3,
                        "RevenueCode": "200"
                    },
                    {
                        "ClaimLine": 4,
                        "RevenueCode": "300"
                    },
                    {
                        "ClaimLine": 5,
                        "RevenueCode": "400"
                    }
                ]
            },
            {
                "EncounterID": "Enc02",
                "EncounterType": "Outpatient"
            }
        ]
    },
    {
        "PatientID": "PID02",
        "PatientName": "Jane Doe",
        "PatientGroup": "Group B",
        "Encounters": [
            {
                "EncounterID": "Enc03",
                "EncounterType": "Inpatient"
            },
            {
                "EncounterID": "Enc04",
                "EncounterType": "Outpatient",
                "ClaimLines": [
                    {
                        "ClaimLine": 1,
                        "RevenueCode": "001"
                    },
                    {
                        "ClaimLine": 2,
                        "RevenueCode": "100"
                    }
                ]
            },
            {
                "EncounterID": "Enc05",
                "EncounterType": "SNF"
            }
        ]
    }
]

任何/所有帮助将不胜感激。如前所述,我不反对在R之外使用工具。

谢谢!

我正在尝试使用R生成嵌套的JSON文件,并且在设置它时遇到了困难。我承认对JSON非常陌生,因此从概念上讲我落后了。我将不胜感激,包括...

r arrays json object jsonlite
2个回答
0
投票

这似乎很接近

library(tidyverse)
library(jsonlite)

json <- reduce(list(
    patients %>% mutate_if(is.factor, as.character),
    encounters %>% mutate_if(is.factor, as.character),
    encounterLines %>%
        mutate_if(is.factor, as.character) %>%
        group_by(EncounterID) %>%
        nest() %>%
        rename(ClaimLines = data) %>%
        mutate(ClaimLines = map(ClaimLines, transpose))),
    left_join) %>%
    nest(Encounters = c(EncounterID, EncounterType, ClaimLines)) %>%
    transpose() %>%
    toJSON(pretty = TRUE)
#[
#  {
#    "PatientID": ["PID01"],
#    "PatientName": ["John Doe"],
#    "PatientGroup": ["Group A"],
#    "Encounters": [
#      {
#        "EncounterID": "Enc01",
#        "EncounterType": "Outpatient",
#        "ClaimLines": [
#          {
#            "RevCodes": ["001"],
#            "ClaimLine": [1]
#          },
#          {
#            "RevCodes": ["100"],
#            "ClaimLine": [2]
#          },
#          {
#            "RevCodes": ["200"],
#            "ClaimLine": [3]
#          },
#          {
#            "RevCodes": ["300"],
#            "ClaimLine": [4]
#          },
#          {
#            "RevCodes": ["400"],
#            "ClaimLine": [5]
#          }
#        ]
#      },
#      {
#        "EncounterID": "Enc02",
#        "EncounterType": "Outpatient",
#        "ClaimLines": {}
#      }
#    ]
#  },
#  {
#    "PatientID": ["PID02"],
#    "PatientName": ["Jane Doe"],
#    "PatientGroup": ["Group B"],
#    "Encounters": [
#      {
#        "EncounterID": "Enc03",
#        "EncounterType": "Inpatient",
#        "ClaimLines": {}
#      },
#      {
#        "EncounterID": "Enc04",
#        "EncounterType": "Outpatient",
#        "ClaimLines": [
#          {
#            "RevCodes": ["001"],
#            "ClaimLine": [1]
#          },
#          {
#            "RevCodes": ["100"],
#            "ClaimLine": [2]
#          }
#        ]
#      },
#      {
#        "EncounterID": "Enc05",
#        "EncounterType": "SNF",
#        "ClaimLines": {}
#      }
#    ]
#  }
#]
#

0
投票

几个data.table加入,你也可以到达那里

© www.soinside.com 2019 - 2024. All rights reserved.