6
$\begingroup$

I am looking for a straightforward way to export results from functions like ImageContents, FacialFeatures, and WolframAlpha - symbols that return stuff containing knowledgebase content.

Aside from obvious non-JSON-exportable things like Missing[], Complex, None, etc. exporting Quantities and Entities look like the main hurdle.

Is there a standard way to replace them with common name strings and strip uuids so that they are jsonifiable as well as understandable to the non-wolfram world?

$\endgroup$

1 Answer 1

8
$\begingroup$

You can use "ConversionFunction" / "ConversionRules" for "RawJSON" export and define whatever you want, and as a quick start you can do:

ExportString[
 {1, Quantity[1, "Meters"], Missing[], 1 - I, None}
, "RawJSON"
, "Compact" -> 1
, "ConversionFunction" -> (
    <|"type" -> "ExpressionJSON", 
      "value" -> ImportString[ 
        ExportString[#, "ExpressionJSON", "Compact" -> True]
      , "JSON"]
    |> &
  )
 
 ]
[
    1,
    {"type":"ExpressionJSON","value":["Quantity",1,"'Meters'"]},
    {"type":"ExpressionJSON","value":["Missing"]},
    {"type":"ExpressionJSON","value":["Complex",1,-1]},
    {"type":"ExpressionJSON","value":"None"}
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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