How to convert JSON to data frame in R without sorting keys

49 Views Asked by At

Input:

json_string <- '{
    "Chamber": ["Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber", "Target Chamber"],
    "P1": [0.5193, 0.5409, 0.5616, 0.5589, 0.5518, 0.5527, 0.5497, 0.5535, 0.5539, 0.5549, 0.5508, 0.5538, 0.5445, 0.5512, 0.554, 0.5586, 0.5569, 0.5539, 0.5583, 0.5606, 0.5549, 0.5617, 0.5433, 0.5435, 0.5441, 0.5564, 0.5436, 0.5458, 0.5546, 0.5373, 0.533, 0.5373, 0.5399, 0.5547, 0.5601, 0.5416, 0.5497, 0.5393, 0.5595],
    "P9": [0.5155, 0.5334, 0.5599, 0.5512, 0.5456, 0.5454, 0.5466, 0.5495, 0.5471, 0.5546, 0.5437, 0.5491, 0.5424, 0.5465, 0.5495, 0.5594, 0.554, 0.5528, 0.555, 0.5518, 0.5573, 0.5596, 0.5391, 0.5553, 0.5441, 0.5597, 0.559, 0.5612, 0.5485, 0.5507, 0.5442, 0.5373, 0.545, 0.5646, 0.569, 0.5531, 0.5602, 0.5567, 0.5737],
    "P10": [0.5155, 0.5334, 0.5599, 0.5512, 0.5456, 0.5454, 0.5466, 0.5495, 0.5471, 0.5546, 0.5437, 0.5491, 0.5424, 0.5465, 0.5495, 0.5594, 0.554, 0.5528, 0.555, 0.5518, 0.5573, 0.5596, 0.5391, 0.5553, 0.5441, 0.5597, 0.559, 0.5612, 0.5485, 0.5507, 0.5442, 0.5373, 0.545, 0.5646, 0.569, 0.5531, 0.5602, 0.5567, 0.5737],
    "P11": [0.5155, 0.5334, 0.5599, 0.5512, 0.5456, 0.5454, 0.5466, 0.5495, 0.5471, 0.5546, 0.5437, 0.5491, 0.5424, 0.5465, 0.5495, 0.5594, 0.554, 0.5528, 0.555, 0.5518, 0.5573, 0.5596, 0.5391, 0.5553, 0.5441, 0.5597, 0.559, 0.5612, 0.5485, 0.5507, 0.5442, 0.5373, 0.545, 0.5646, 0.569, 0.5531, 0.5602, 0.5567, 0.5737],
    "P12": [0.5149, 0.5375, 0.5556, 0.5517, 0.5456, 0.5451, 0.5428, 0.5479, 0.5495, 0.5534, 0.5434, 0.5486, 0.5415, 0.5462, 0.5478, 0.5597, 0.5494, 0.549, 0.5464, 0.5513, 0.5561, 0.5664, 0.5432, 0.5557, 0.5482, 0.5582, 0.5537, 0.5658, 0.559, 0.546, 0.5372, 0.5378, 0.5484, 0.5612, 0.5656, 0.5495, 0.5649, 0.5638, 0.579]
}'
1

There are 1 best solutions below

0
On

Is it this what you're looking for?

df <- data.frame(jsonlite::fromJSON(json_string))
head(df)
#>          Chamber     P1     P9    P10    P11    P12
#> 1 Target Chamber 0.5193 0.5155 0.5155 0.5155 0.5149
#> 2 Target Chamber 0.5409 0.5334 0.5334 0.5334 0.5375
#> 3 Target Chamber 0.5616 0.5599 0.5599 0.5599 0.5556
#> 4 Target Chamber 0.5589 0.5512 0.5512 0.5512 0.5517
#> 5 Target Chamber 0.5518 0.5456 0.5456 0.5456 0.5456
#> 6 Target Chamber 0.5527 0.5454 0.5454 0.5454 0.5451