1

I have a pretty straight forward JSON object that I am trying to parse into a list of objects for downstream processing and use. The JSON structure is dynamic but here is an example of the structure I am trying to handle

json_string <- '{
  "instances": [
    {
      "feature_1": [
        { "col1":  "value1", "col2": "value2" },
        { "col1":  "value2", "col2":  "value3" },
        { "col1":  "value4", "col2":  "value5" }
      ],
      "feature_2": "raw_value",
      "feature_3": [ "list_value_1", "list_value_2"],
      "feature_4": {
        "feature_4_prop1": "prop_value_1",
        "feature_4_prop2": "prop_value_2"
      }
    },
    {
      "feature_1": [
        { "col1":  "value1", "col2": "value2" },
        { "col1":  "value2", "col2":  "value3" },
        { "col1":  "value4", "col2":  "value5" }
      ],
      "feature_2": "raw_value",
      "feature_3": [ "only_1_value"],
      "feature_4": {
        "feature_4_prop1": "prop_value_1",
        "feature_4_prop2": "prop_value_2"
      }
    }
  ]
}'

library(jsonlite)
parsed_data <- jsonlite::fromJSON(json_string, simplifyDataFrame=TRUE, flatten=FALSE)
row <- parsed_data$instances[1,]

print("fin")

I want to be able to parse this and then obtain the instances inside of a list (or data.frame) i.e parsed_data$instances

When I view a single row / item of this list via row <- parsed_data$instances[1,] I would like it to assume the following structure for each column.

feature_1 is a data.table with the columns of col_1, col2.
feature_2 is a raw value as indicated.
feature_3 to be a list even if a single value
feature_4 to be an object so that row$feature_4 allows me to access the properties.

I have tried to do this via jsonlite:fromJSON(json_data, simplifyDataFrame=TRUE, flatten=FALSE) where flatten was set to false in case only 1 instance is set.

The issue is when I inspect a row or assign a row I see that feature_1 is given to me as a list and in order to access the table itself I would have to do something like the following:

row <- parsed_data$instances[1,]
feature_1_table <- row[['feature_1']][[1]]

Is there a way that I can parse a JSON structure above to achieve my desired results?

edit: Including Screenshot of debugger to show that jsonlite is not doing this out of the box and that feature_1 is showing as a property that is list of length 1.

enter image description here

3
  • 2
    when i run sonlite:fromJSON(json_data)$instances i get the table the exact format you are describing. Run your code again Commented Oct 10, 2023 at 18:33
  • @Onyambu I have edited the original post to include an image showing that the value of feature_1 is a list that contains a table rather than just a table. Commented Oct 10, 2023 at 19:26
  • Note that a column can only contain tables if its wrapped in a list. All columns whose mode/type is not of base datatypes ie character, logical, numeric,raw must be encapsulated in a list Commented Oct 10, 2023 at 19:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.