Skip to contents

From hospital JSON to a database

Under CMS Hospital Price Transparency (HPT) requirements, hospitals publish machine-readable files containing charges, payer-specific terms, and related context. A JSON file can be large and deeply nested: one service entry may contain several codes, charge contexts, and payer terms. Those entries are difficult to explore as one long JSON document.

cmsmrf extracts supported files into a persistent DuckDB database. Each table represents one kind of source record, with keys that retain its place in the original file. You can query the database later with ordinary SQL and DBI, without loading the whole JSON file into R. The current package supports HPT JSON schema V3.0.0, informed by data dictionary v3.0. mrf_specification() identifies the pinned CMS artifacts used to develop that support:

reference <- mrf_specification()
reference[c("family", "format", "schema_version", "dictionary_version",
            "reference_date", "repository_commit")]
#> $family
#> [1] "CMS Hospital Price Transparency (HPT)"
#> 
#> $format
#> [1] "JSON"
#> 
#> $schema_version
#> [1] "3.0.0"
#> 
#> $dictionary_version
#> [1] "3.0"
#> 
#> $reference_date
#> [1] "2026-11-27"
#> 
#> $repository_commit
#> [1] "6855f04377ec6e87f88798cd618a27a826eebd2a"

The reference identifies the expected source structure; it does not establish that any particular file complies with CMS rules. The package does not acquire files, normalize services or payer identities, estimate patient bills, or turn every pricing method into a comparable dollar price.

Meet the two fictional sources

Two small, fictional JSON files let you learn the workflow without downloading hospital data. The basic file has a short hospital disclosure with services, codes, charges, and negotiated dollars. The detailed file adds repeated service descriptions, multiple codes and charges, drug information, modifiers, contract provisions, percentage and algorithm terms, and historical allowed amounts. Their hospital, payer, plan, and price details are invented for teaching; they are not estimates of real prices or an exhaustive set of CMS structures.

mrf_example() locates the installed files. Its paths can also be opened in your preferred text editor to see the original JSON.

paths <- mrf_example()
data.frame(example = names(paths), file = basename(paths), row.names = NULL)
#>    example                   file
#> 1    basic    hospital-basic.json
#> 2 detailed hospital-detailed.json
basename(mrf_example("basic"))
#> [1] "hospital-basic.json"
basename(mrf_example("detailed"))
#> [1] "hospital-detailed.json"

Inspect, then extract

Inspection offers a small overview before you commit to extraction. It reports what the file declares, including its version and service count. Here the detailed file declares version 3.0.0, has four service entries, and includes optional contract provisions and modifiers.

basic <- mrf_inspect(mrf_example("basic"))
detailed <- mrf_inspect(mrf_example("detailed"))
basic[c("hospital_name", "last_updated_on", "declared_version",
        "supported_schema_version", "supported_version", "file_size_bytes",
        "service_count")]
#> $hospital_name
#> [1] "Fictional Willowmere Community Hospital"
#> 
#> $last_updated_on
#> [1] "2026-11-15"
#> 
#> $declared_version
#> [1] "3.0.0"
#> 
#> $supported_schema_version
#> [1] "3.0.0"
#> 
#> $supported_version
#> [1] TRUE
#> 
#> $file_size_bytes
#> [1] 2775
#> 
#> $service_count
#> [1] 2
detailed[c("hospital_name", "service_count", "has_contract_provisions",
           "has_modifiers")]
#> $hospital_name
#> [1] "Fictional Maplebrook Regional Hospital"
#> 
#> $service_count
#> [1] 4
#> 
#> $has_contract_provisions
#> [1] TRUE
#> 
#> $has_modifiers
#> [1] TRUE

supported_version compares the file’s declared version with the version the package recognizes; it is not a conformance result. Inspection creates no persistent database and is optional: you can pass paths directly to mrf_parse().

The next call processes both files, in the order returned by mrf_example(). The first attempt receives file_id = 1, and the second receives file_id = 2. Every supplied occurrence gets its own ID, even if a path is repeated or an attempt fails. We use a temporary output for this guide; for your own data, choose a new persistent filename in an existing directory. An existing output file is never overwritten.

database <- tempfile(fileext = ".duckdb")
created <- mrf_parse(path = paths, output = database, quiet = TRUE)
identical(created, normalizePath(database))
#> [1] TRUE

The operation returns that path invisibly when it completes. quiet = TRUE hides routine progress messages. Completion alone does not mean every input succeeded; check the database’s per-file outcomes next.

Reopen and check processing outcomes

The database is a file that can be reopened in a later R session or with a compatible DuckDB client. Read-only access prevents exploratory queries from changing it. The source JSON and the R object created are not needed to query a completed database.

con <- suppressMessages(DBI::dbConnect(
  duckdb::duckdb(), dbdir = database, read_only = TRUE
))
sort(DBI::dbListTables(con))
#>  [1] "charge"             "charge_modifier"    "contract_provision"
#>  [4] "database_metadata"  "diagnostic"         "file_location"     
#>  [7] "file_npi"           "file_summary"       "modifier"          
#> [10] "modifier_payer"     "mrf_file"           "payer_charge"      
#> [13] "service"            "service_code"       "service_drug"      
#> [16] "source_file"

The 16 relations include four processing tables. database_metadata records package and specification context; source_file records each attempted input, its available byte fingerprint, and its terminal status; file_summary counts materialized rows in each of the 12 source-derived relations for successful files; and diagnostic holds consequential findings encountered during extraction.

knitr::kable(DBI::dbGetQuery(con, "
  SELECT package_version, specification_format, schema_version, dictionary_version
  FROM database_metadata
"))
package_version specification_format schema_version dictionary_version
0.0.0.9000 JSON 3.0.0 3.0
knitr::kable(DBI::dbGetQuery(con, "
  SELECT file_id, file_name, status
  FROM source_file ORDER BY file_id
"))
file_id file_name status
1 hospital-basic.json success
2 hospital-detailed.json success
knitr::kable(DBI::dbGetQuery(con, "
  SELECT file_id, row_count AS service_rows
  FROM file_summary
  WHERE relation_name = 'service'
  ORDER BY file_id
"))
file_id service_rows
1 2
2 4
knitr::kable(DBI::dbGetQuery(con, "SELECT count(*) AS recorded_findings FROM diagnostic"))
recorded_findings
0

Both examples succeeded, with two and four materialized service rows respectively. Zero recorded findings means these files triggered none of the implemented reactive diagnostics. It does not mean the sources are error-free or CMS-compliant. Missing optional information ordinarily needs no finding. On another input, a completed database can contain failed file attempts, so inspect source_file before treating its source tables as complete.

Follow one source record into its tables

The basic file’s first service contains the following exact excerpt. It reports one CPT code, a charge with gross and cash amounts, and one payer-specific negotiated dollar amount. The excerpt is read from the installed JSON, not assembled from database rows.

      "description": "Complete blood count with differential",
      "code_information": [
        {
          "code": "85025",
          "type": "CPT"
        }
      ],
      "standard_charges": [
        {
          "setting": "outpatient",
          "gross_charge": 82.0,
          "discounted_cash": 58.0,
          "minimum": 52.0,
          "maximum": 52.0,
          "payers_information": [
            {
              "payer_name": "Fictional Cedar Health",
              "plan_name": "Cedar Standard",
              "methodology": "fee schedule",
              "standard_charge_dollar": 52.0
            }
          ]
        }
      ]

The service row represents that one source service entry. Its service_code child represents the code occurrence. The package assigns service_id = 1 from the first service’s position in the basic file; CMS did not supply this as a real-world service identifier.

knitr::kable(DBI::dbGetQuery(con, "
  SELECT s.file_id, s.service_id, s.description, sc.code, sc.code_type
  FROM service AS s
  JOIN service_code AS sc
    ON sc.file_id = s.file_id AND sc.service_id = s.service_id
  WHERE s.file_id = 1 AND s.service_id = 1
"))
file_id service_id description code code_type
1 1 Complete blood count with differential 85025 CPT

The charge and payer term are separate child records. Their join uses the complete parent key: file_id, service_id, and charge_id. The amounts below match the excerpt: gross 82, discounted cash 58, and negotiated dollar 52.

knitr::kable(DBI::dbGetQuery(con, "
  SELECT ch.charge_id, ch.gross_charge, ch.discounted_cash,
         p.payer_name, p.standard_charge_dollar
  FROM charge AS ch
  JOIN payer_charge AS p
    ON p.file_id = ch.file_id AND p.service_id = ch.service_id
   AND p.charge_id = ch.charge_id
  WHERE ch.file_id = 1 AND ch.service_id = 1
  ORDER BY ch.charge_id, p.payer_id
"))
charge_id gross_charge discounted_cash payer_name standard_charge_dollar
1 82 58 Fictional Cedar Health 52

Each table has a grain: one row means one file, service occurrence, code occurrence, charge occurrence, or payer term, depending on the table. service_id starts again in the second file, so it identifies a service only together with file_id. charge_id starts again within each service. These keys preserve source relationships; repeated descriptions or billing codes do not establish interchangeable real-world services. The HPT JSON Relational Data Model gives the complete relation, column, and key definitions.

Ask a source-aware question

The detailed file reports CPT code 70551 under two separate “Brain MRI without contrast” service entries. One has facility charges in outpatient and inpatient settings; the other has a professional charge. This query finds charges for services that have that code. EXISTS filters services without multiplying charge rows by their code entries.

knitr::kable(DBI::dbGetQuery(con, "
  SELECT s.file_id, s.service_id, ch.charge_id,
         ch.setting, ch.billing_class, ch.gross_charge
  FROM service AS s
  JOIN charge AS ch
    ON ch.file_id = s.file_id AND ch.service_id = s.service_id
  WHERE EXISTS (
    SELECT 1 FROM service_code AS sc
    WHERE sc.file_id = s.file_id AND sc.service_id = s.service_id
      AND sc.code_type = 'CPT' AND sc.code = '70551'
  )
  ORDER BY s.file_id, s.service_id, ch.charge_id
"))
file_id service_id charge_id setting billing_class gross_charge
2 1 1 outpatient facility 1800
2 1 2 inpatient facility NA
2 3 1 outpatient professional 320

Combining all codes, charges, and payer terms under a service in one join can multiply rows across separate child arrays. Such combinations are not necessarily separately disclosed prices. Select the grain and context needed for the question, and join on every parent-key column.

The inpatient facility row has no reported gross charge. Its NA is missing information, not a zero-dollar charge.

The detailed source also shows why reported pricing forms need separate interpretation. Its first service’s inpatient charge has a 70.5 percent term for one payer and an algorithm for another. Historical median allowed amounts appear alongside them; they are not negotiated dollar prices.

knitr::kable(DBI::dbGetQuery(con, "
  SELECT payer_id, methodology,
         standard_charge_percentage AS percent,
         CASE WHEN standard_charge_algorithm IS NULL THEN 'no' ELSE 'yes' END
           AS has_algorithm,
         allowed_amount_count, median_amount
  FROM payer_charge
  WHERE file_id = 2 AND service_id = 1 AND charge_id = 2
  ORDER BY payer_id
"))
payer_id methodology percent has_algorithm allowed_amount_count median_amount
1 percent of total billed charges 70.5 no 12 1350
2 other NA yes 1 through 10 980

Gross charges are not necessarily actual payments; cash prices are distinct from payer terms; negotiated dollars are not necessarily insurer-only payments. A percentage is not a dollar amount without its contractual base, and an algorithm may need facts that the source cannot know in advance. Historical allowed-amount statistics describe prior claims, not a future bill. Do not compare unlike forms as if they were the same price, or treat matching payer labels as resolved payer identities. Consult the data-model guide for field meanings, pricing distinctions, and limitations.

Use your own supported files

For hospital files you obtain yourself, use the same functions with your local paths and a database filename you intend to retain. This illustrative code requires your own files and is not run when the vignette builds:

mrf_inspect("hospital-a.json")
mrf_parse(c("hospital-a.json", "hospital-b.json"),
          output = "hospital-prices.duckdb")

The package does not download sources. See ?mrf_example for a short executable demonstration, ?mrf_parse for extraction outcomes, and vignette("data-model", package = "cmsmrf") for the full relational contract.

Close the read-only connection after querying. We also delete this guide’s temporary database; keep your own persistent output when you want to return to it.

DBI::dbDisconnect(con)
unlink(database)
file.exists(database)
#> [1] FALSE