0

I am trying to call an API from R programming and i got successful in GET method but when i am reading an excel file and converting it into JSON i am getting error.

Please find the code below:

JSON should look like this :

{
"internalId": "Asset Name",
"description": {
"short": "Asset Type",
"long": "Asset Type"
},
"dataType": "numeric",
"optionCode": "1",
"dimension1": null,
"dimension2": null,
}

But after conversion it looks like :

[
  {
    "internalId": "RAttribute",
    "description.language": "en",
    "description.short": "R Attribute",
    "dataType": "string",
    "optionCode": 3
  }
]

Dataset from where i am loading : Dataset

Code for loading the dataset :

jsonAttributes <- jsonlite::toJSON(Attributes,pretty=TRUE)
print(jsonAttributes)

Please help here.... let me know if any other information is required.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Please do not post images of the dataset. At the very least provide a text sample. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Sada93 Feb 19 '19 at 06:12
  • You lost me at the stated convergence of a GET/POST API on an Excel file. If you're talking about HTTP's get/post methods, or even if you're not, I don't see a connection. When json doesn't look like you expect, it has nothing to do with `jsonlite`, and possibly nothing to do with HTTP per se, likely to do with the service providing the API and how you are interacting with it. – r2evans Feb 19 '19 at 06:36
  • Thanks for the reply r2evans ! Totally agree with your point.. conversion from excel to json will be like that only but i want to check if there is a separator which can do the nesting at the time of conversion from Dataset to json?? If you can see... i am using "." as a separator in excel to do the nesting but its not working... so is there any predefined format in which data should be available before conversion to json ? – Sunny Manghnani Feb 19 '19 at 06:46
  • What does "excel" have with "API? Is there a web service somewhere that is serving excel documents as JSON? How are you calling it? – r2evans Feb 20 '19 at 04:58
  • Process which i am following is : 1. Loading the data into Excel (it can be from any source or API or Manual Load) 2. Converting the Excel into JSON 3. Posting the JSON. I am having issues on json conversion as it is not getting converted into API specific format as it requires nesting as you can see in first block of code above (Description Column) – Sunny Manghnani Feb 20 '19 at 05:25
  • Somehow i manually tried and converted into the output require. Please find the code below : x<- list( internalId = Attributes$internalId, description = list(language = Attributes$description.language, short = Attributes$description.short, long = Attributes$description.long), dimension1 = Attributes$dimension1 ) cat(RJSONIO::toJSON(x)) But this process will be a manual work :( – Sunny Manghnani Feb 20 '19 at 05:27
  • You still lost me at step 2. In my MS Excel 2016, *"Save as ..."* offers many formats but not JSON. – r2evans Feb 20 '19 at 17:55
  • In Step 2 : I am saving my data in excel.. .then importing that excel into R and then converting the imported dataset into json... Hope this answers your question – Sunny Manghnani Feb 21 '19 at 03:22

0 Answers0