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$
2
  • $\begingroup$ this is a great answer but could we reformat this so someone who knows nothing about Wolfram can understand it? For example, the entities for concepts like Happiness have weird uuids in them. $\endgroup$
    – M.R.
    Commented Jul 5, 2022 at 16:17
  • 1
    $\begingroup$ @M.R. we could make it produce "[\"Entity\",\"'Emotion'\",\"'Happiness'\"]" but it won't be reversible. Could be fine if you don't want to import it back. I can add lines that address it but you'd need to provide more examples because I don't work with entities and I don't know if the id in the second argument is all that I should take care of. $\endgroup$
    – Kuba
    Commented Jul 6, 2022 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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