How to convert jCard into first-class JSON?

317 views Asked by At

I need a stable and secure convertion algorithm (any language), that can produce the final output as "first class" JSON objects. Example:

  • jCard format: [["version", {}, "text", "4.0"],["fn", {}, "text", "Forrest Gump"]]

  • First-class JSON format: {"version":"4.0","fn":"Forrest Gump"}.

2

There are 2 answers

0
Parthish On

In python first, I create one function named jcard_to_json that takes a jCard as input and converts it to a "first-class" JSON object. The function iterates over the items in the jCard, and for each item, it adds a key-value pair to the json_obj dictionary, where the key is the first item in the jCard thing and the value is the fourth item

Example:-

def jcard_to_json(jcard):
json_obj = {}
for item in jcard:
    json_obj[item[0]] = item[3]
return json_obj

jcard = [["version", {}, "text", "4.0"], ["fn", {}, "text", "Forrest 
Gump"]]
json_obj = jcard_to_json(jcard)
0
Juraj On

In Java.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JCardTest {

  public static void main(String[] args) throws JSONException {
    
    String jcardStr = "[\"vcard\","
        + "     ["
        + "       [\"version\", {}, \"float\", \"4.0\"],"
        + "       [\"fn\", {}, \"text\", \"John Doe\"],"
        + "       [\"gender\", {}, \"text\", \"M\"],"
        + "       [\"categories\", {}, \"text\", \"computers\", \"cameras\"],"
        + "       [\"number\", {}, \"integer\", 12345],"
        + "       [\"adr\","
        + "          { \"type\": \"work\" },"
        + "          \"text\","
        + "          ["
        + "           \"\","
        + "           \"Suite D2-630\","
        + "           \"2875 Laurier\","
        + "           \"Quebec\","
        + "           \"QC\","
        + "           \"G1V 2M2\","
        + "           \"Canada\""
        + "          ]"
        + "       ]"
        + "     ]"
        + "   ]";

    JSONArray jcard = new JSONArray(jcardStr);
    jcard = jcard.getJSONArray(1);
    JSONObject result = new JSONObject();
    for (int i = 0; i < jcard.length(); i++) {
      JSONArray arr = jcard.getJSONArray(i);
      String name = arr.getString(0);
      String dataType = arr.getString(2);
      if (arr.length() == 4) {
        switch (dataType) {
          case "integer": {
            long val = arr.getLong(3);
            result.put(name, val);
          }
          break;
          case "float": {
            double val = arr.getDouble(3);
            result.put(name, val);
          }
          break;
          default:
            Object val = arr.get(3);
            if (val instanceof JSONArray) {
              result.put(name, (JSONArray) val);
            } else {
              result.put(name, val.toString());
            }
          break;
        }
      } else {
        JSONArray resArr = new JSONArray();
        for (int j = 3; j < arr.length(); j++) {
          resArr.put(arr.get(j).toString());
        }
        result.put(name, resArr);
      }
    }
    System.out.println(result);
  }

}

  • This ignores the 'parameter" part (arr.get(1)) of the jCard entries.
  • the code is verbose to not to hide important details. it could be written more compact.
  • Example is based on examples in the jCard RFC7095.