0

I have a similar flatten JSON file and i would like to create a nested field for "Material_name_lv1", "Material_name_lv2", and "Material_name_lv3".

[
  {
    "Vendor": 100000,
    "Vendor.Name": "xxxCARS",
    "PO.Number": 4100004621,
    "Prod.Desc": "Taxi",
    "Date": "04/01/2014",
    "Material.group": "CO1250106",
    "Material.Name": "Corporate - Travel - Taxis",
    "PO.Item": 1,
    "Requester": "65",
    "Requester.Name": "Anna",
    "Payment.Terms": "YT",
    "Order..Value": 100,
    "GR..Value": 0,
    "GR..": 0,
    "Invoiced..Amount": 100,
    "Invoiced..": 100,
    "material_name_lv1": "Corporate ",
    "material_name_lv2": " Travel ",
    "material_name_lv3": " Taxis"
  },
  {
    "Vendor": 100000,
    "Vendor.Name": "xxx CARS",
    "PO.Number": 4100011066,
    "Prod.Desc": "Taxi when and as required",
    "Date": "02/01/2015",
    "Material.group": "CO1250106",
    "Material.Name": "Corporate - Travel",
    "PO.Item": 1,
    "Requester": "65",
    "Requester.Name": "Anna",
    "Payment.Terms": "YT",
    "Order..Value": 500,
    "GR..Value": 0,
    "GR..": 0,
    "Invoiced..Amount": 500,
    "Invoiced..": 100,
    "material_name_lv1": "Corporate ",
    "material_name_lv2": " Travel ",
    "material_name_lv3": ""
  }
]

I would like "Material_name_lv3" to be a nested field of "Material_name_lv2" and "Material_name_lv2" to be a nested field of "Material_name_lv1".

eg:-

{
    "Vendor": 100000,
    "Vendor.Name": "500 CARS",
    "PO.Number": 4100004621,
    "Prod.Desc": "Taxi",
    "Date": "04/01/2014",
    "Material.group": "CO1250106",
    "Material.Name": "Corporate - Travel - Taxis",
    "PO.Item": 1,
    "Requester": "65",
    "Requester.Name": "Kurzawa, Anna",
    "Payment.Terms": "YP30",
    "Order..Value": 10000,
    "GR..Value": 0,
    "GR..": 0,
    "Invoiced..Amount": 10000,
    "Invoiced..": 100,
    "material_name_lv1":{
      "name" : "coperate",
        "material_name_lv2": {
          "name": "Travel",
            material_name_lv3": {
              "name": "Taxis"
          }
        },
      },
  },

I am quite new to R and never worked on JSON files. I found a bunch of R packages that handles JSON but nothing solid on formatting commands. Any guidance would be appreciated.

NB:- i split the material_name levels from "Material.Name" and certain rows contain only 2 levels.

Thanks

1 Answer 1

1

You just have to create a list in the same form as you want the JSON output...

mylist <- list(a="something",
               b=list("name"="myname",
                      "material_name"=list("name"="mat_name",
                                           "age"=12
                                           )
               )
)

> jsonlite::toJSON(mylist, pretty=TRUE, auto_unbox=TRUE)
{
  "a": "something",
  "b": {
    "name": "myname",
    "material_name": {
      "name": "mat_name",
      "age": 12
    }
  }
} 
Sign up to request clarification or add additional context in comments.

Comments

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.