Extract property from json with ScriptEvalValve

Sometimes data is returned from an external system in json-format. This document will give an example of how to extract data from a property containing json to an item property.

ScriptEvalValve is used to extract the data.

Example

The response of the external system i stored in response.

The value to extract is mbr_email.

{
  "id" : "dummy",
  "properties" : {
    "response" : [ "{\"mbr_firstname\":\"Arne\",\"mbr_surname\":\"Anka\",\"mbr_status\":\"A\",\"mbr_birthdate\":\"1971-06-12\",\"mbr_address\":\"\",\"mbr_address2\":\"\",\"mbr_country\":\"\",\"mbr_post_code\":\"\",\"mbr_city\":\"\",\"mbr_municipality\":null,\"mbr_county\":null,\"mbr_phone\":\"\",\"mbr_mobile\":\"0701234\",\"mbr_sex\":\"M\",\"mbr_email\":\"dummy@dummy.com\",\"mbr_terms_confirmed\":true,\"mbr_mobile_verified\":true,\"membership\":null,\"subscr_id\":null,\"labels\":null,\"custom_fields\":[{\"param_attr_id\":18,\"param_value\":\"1\"},{\"param_attr_id\":19,\"param_value\":\"1\"},{\"param_attr_id\":1262,\"param_value\":\"\"},{\"param_attr_id\":1263,\"param_value\":\"\"},{\"param_attr_id\":1264,\"param_value\":\"\"},{\"param_attr_id\":1265,\"param_value\":\"\"},{\"param_attr_id\":1266,\"param_value\":\"\"},{\"param_attr_id\":1267,\"param_value\":\"\"},{\"param_attr_id\":1268,\"param_value\":\"\"},{\"param_attr_id\":1272,\"param_value\":\"\"},{\"param_attr_id\":1273,\"param_value\":\"66666666\"}]}" ],
	"sn" : [ "Anka" ]
  }
}

Configuration

	{
				"name": "ScriptEvalValve",
				"config": {
					"mime_type": "application/javascript",
					"script": "var string = flow.items().get(0).getPropertyValue('response'); var json = JSON.parse(string); var mbr_email = json['mbr_email']; flow.items().get(0).replaceProperty('mbr_email', mbr_email);"
				}
			},