Message Boards Message Boards

GROUPS:

Convert Wolfram Dataset to JSON or CSV via API?

Posted 2 months ago
466 Views
|
5 Replies
|
2 Total Likes
|

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?

POSTED BY: Sag Mk
Answer
5 Replies

Maybe

Export[filename, dataset,  "CSV"]

??

Posted 2 months ago

Getting $Failed error without any specifications

POSTED BY: Sag Mk
Answer
Posted 2 months ago

This works with direct dataset names, but I want to expose it as and API call where I can insert any table name and get CSV or JSON response from the CloudDeploy API

POSTED BY: Sag Mk
Answer
Posted 2 months ago

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"
POSTED BY: Sag Mk
Answer

So you need some predefined rules to convert Mathematica objects listed in the screenshot to either list of rules or associations.Check the attached notebook for code.

conversion

Since ExportForm takes the same options as Export, the following code automatically works

CloudDeploy@ExportForm[input, "JSON",
  "ConversionRules" -> {
    Quantity[magnitude_, unit_] :> <|"magnitude" -> magnitude, "unit" -> unit|>,
    DateObject[{ele_}, others__] :>  <|"Year" -> ele|>,
    GeoPosition[{lat_, lon_}] :> <|"latitute" -> lat, "longitute" -> lon |>
    }
  ]
Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract