I am getting wolfram CDF format from this,

beta = APIFunction[{"tablename" -> "String"},ResourceData[ResourceObject[#tablename] ]& ]
co = CloudDeploy[beta, Permissions->"Public"]

Response:

Dataset[{<|"Name" -> "Aachen", "ID" -> "1", "NameType" -> "Valid", "Classification" -> "L5", "Mass" -> Quantity[21, "Grams"], "Fall" -> "Fell", "Year" -> DateObject[{1880}, "Year", "Gregorian", -5.], "Coordinates" -> GeoPosition[{50.775, 6.08333}]|>, <|"Name" -> "Aarhus", "ID" -> "2", "NameType" -> "Valid", "Classification" -> "H6", "Mass" -> Quantity[720, "Grams"], "Fall" -> "Fell", "Year" -> DateObject[{1951}, "Year", "Gregorian", -5.], "Coordinates" -> GeoPosition[{56.18333, 10.23333}]|>, <|"Name" -> "Abee", "ID" -> "6", "NameType" -> "Valid", "Classification" -> "EH4", "Mass" -> Quantity[107000, "Grams"], "Fall" -> "Fell", "Year" -> DateObject[{1952}, "Year", "Gregorian", -5.], "Coordinates" -> GeoPosition[{54.21667, -113.}]|>, <|"Name" -> "Acapulco", "ID" -> "10", "NameType" -> "Valid", "Classification" -> "Acapulcoite", "Mass" -> Quantity[1914, "Grams"], "Fall" -> "Fell", "Year" -> DateObject[{1976}, "Year", "Gregorian", -5.], "Coordinates" -> GeoPosition[{16.88333, -99.9}]|>, <|"Name" -> "Achiras", "ID" -> "370", "NameType" -> "Valid", "Classification" -> "L6", "Mass" -> Quantity[780, "Grams"], "Fall" -> "Fell", "Year" -> DateObject[{1902}, "Year", "Gregorian", -5.], "Coordinates" -> GeoPosition[{-33.16667, -64.95}]|> }]

I need this in a JSON format, I tried to convert it using URLexecute it didn't work

Does anyone know any pythonic or wolfram way to convert this into JSON or CSV?

New contributor
Mk47 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

What you are getting is not "CDF format" but a Mathematica Dataset. You should also note that you are trying to convert and serve a dataset that has 45716 entries. This might well run into limits (cputime, memory, transfered data) of an APIFunction. I am also not sure whether WRI will allow you to provide a possibility to download complete resource-data datasets, you would have to check their legal conditions. Finally you should note that there is already a possibility to download the whole dataset from https://datarepository.wolframcloud.com/resources/Meteorite-Landings, although the JSON that is delivered looks a bit special (it is a Mathematica expression expressed as JSON arrays, probably not what one would expect...).

Using the following code in your API function should convert the first 10 entries into a JSON equivalent of the Dataset:

api=APIFunction[{"tablename" -> "String"},ExportString[
 (Take[ResourceData[ResourceObject[#tablename]],10] // Normal) //. {
 a_Association :> Normal[a],
 q_Quantity :> QuantityMagnitude[q],
 d_DateObject :> d@"Year",
 g_GeoPosition :> {Latitude[g], Longitude[g]}
}, "JSON","Compact"->True] &,"String"]

What it does is to convert the Dataset into a list of rules using Normal (as ExportString[_,"JSON"] can not directly digest the Dataset). It then converts all elements that can not directly be exported to something equivalent that can be exported. Check the documentation for more details about the various data types and functions used. I also added the option "Compact"->True for ExportString which will make the result more compact (it avoids formatting with tabs and newlines) and an additional explicit format argument to APIFunction which will make the APIFunction return the JSON string itself instead of a Mathematica string containing the JSON string (which basically means an extra set of "). This way the APIFunction should behave more like one would expect.

As you seem to have problems to get the APIFunction to work, here is example code which shows how to deploy the APIFunction:

co = CloudDeploy[api, Permissions -> "Public"];

... check that it works correctly (note that you need to include the space in "tablename" and URL-encode it, which URLBuild will conveniently do for you):

Import[URLBuild[co, {"tablename" -> "Meteorite Landings"}], "RawJSON"]

... and finally remove the public api (you probably don't want it to linger around and let others consume your cloud credits):

DeleteFile[co]

One more tip when working with such code: I would only deploy some functionality to the cloud once it works locally. In your case you could for example test the conversion to JSON entirely on your local machine without spending any cloud credits and resulting in a much faster loop while developing/testing:

data = ResourceData[ResourceObject["Meteorite Landings"]]

Length[data]

Take[data, 10]

ExportString[(Take[data, 10] // Normal) //. {a_Association :> 
  Normal[a], q_Quantity :> QuantityMagnitude[q], 
  d_DateObject :> d@"Year", 
  g_GeoPosition :> {Latitude[g], Longitude[g]}}, "JSON"]
  • 2
    This could also be done inside of ExportString[ds, "RawJSON", "ConversionFunction" -> Normal, "ConversionRules" -> {q_Quantity :> <|"mag" -> QuantityMagnitude[q], "unit" -> QuantityUnit[q]|>, d_DateObject :> d@"Year", g_GeoPosition :> <|"lat" -> Latitude[g], "long" -> Longitude[g]|>}] – chuy Sep 13 at 20:08
  • Hi I am very new to Wolfram alpha I tried gamma = APIFunction[ExportString[(ResourceData[ResourceObject[#tablename]] // Normal) //. { a_Association :> Normal[a], q_Quantity :> QuantityMagnitude[q], d_DateObject :> d@"Year", g_GeoPosition :> {Latitude[g], Longitude[g]} }, "JSON"] & ] the API didnt return anything except the function definition – Mk47 Sep 13 at 20:24
  • The api accepts tablename parameter = "Meteorite Landings" – Mk47 Sep 13 at 20:27
  • @albert-retey can you let me know what am I doing wrong – Mk47 Sep 13 at 21:02
  • Also tried ExportForm getting following error response on it On trying exportForm I am getting error CloudDeploy[ExportForm[ResourceData[ResourceObject["MeteoriteLandings"]],"JSON"]] ] Dataset: Dataset of the given form could not be exported as format "JSON" – Mk47 Sep 13 at 21:26

Your Answer

Mk47 is a new contributor. Be nice, and check out our Code of Conduct.
 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.