Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205278

Nested R dataframe to JSON with objects instead of arrays

$
0
0

I need to convert a dataframe to JSON. There are several nested dataframes as variables in the dataframe to convert to JSON.

But, when converting to JSON, I need the data for Values1 described below to be an object (enclosed in {} only) instead of an array (enclosed in []).

The code below is a reprex to show my current workflow and problem.

library(dplyr)
library(tidyr)
library(jsonlite)

df1 <- data.frame(name = c("a", "b", "c"),
                 v = c(1, 2, 3),
                 w = c(10, 20, 30)) %>%
  group_by(name) %>%
  nest_legacy(.key = "Values1")

df2 <- data.frame(name = c("a", "b", "c"),
                  x = c(5, 10, 15),
                  y = c(100, 200, 300),
                  z = c(1000, 2000, 3000)) %>%
  group_by(name) %>%
  nest_legacy(.key = "Values2")

df3 <- df1 %>%
  left_join(df2)

json <- toJSON(df3, dataframe = "rows", pretty = TRUE)

json

This is what the json from the above looks like:

> json
[
  {
    "name": "a",
    "Values1": [
      {
        "v": 1,
        "w": 10
      }
    ],
    "Values2": [
      {
        "x": 5,
        "y": 100,
        "z": 1000
      }
    ]
  },
  {
    "name": "b",
    "Values1": [
      {
        "v": 2,
        "w": 20
      }
    ],
    "Values2": [
      {
        "x": 10,
        "y": 200,
        "z": 2000
      }
    ]
  },
  {
    "name": "c",
    "Values1": [
      {
        "v": 3,
        "w": 30
      }
    ],
    "Values2": [
      {
        "x": 15,
        "y": 300,
        "z": 3000
      }
    ]
  }
] 

But, this is what I need it to look like:

> json
[
  {
    "name": "a",
    "Values1": {
        "v": 1,
        "w": 10
      },
    "Values2": [
      {
        "x": 5,
        "y": 100,
        "z": 1000
      }
    ]
  },
  {
    "name": "b",
    "Values1": {
        "v": 2,
        "w": 20
      },
    "Values2": [
      {
        "x": 10,
        "y": 200,
        "z": 2000
      }
    ]
  },
  {
    "name": "c",
    "Values1": {
        "v": 3,
        "w": 30
      },
    "Values2": [
      {
        "x": 15,
        "y": 300,
        "z": 3000
      }
    ]
  }
] 

Any ideas how to convert Values1 from arrays to objects? They cannot be in arrays as the API does not accept the [.

I have looked at using unbox() from jsonlite but this only works on singletons. I have also tried to construct my final dataframe in various ways.

Any tips/ideas greatly appreciated!


Viewing all articles
Browse latest Browse all 205278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>