0

How do I convert azure search string to List of search documents. is it possible to convert the below string using jackson. I know this is not a valid JSON.

The below string comes from Azure search and the type is Collection Edm ComplexType.

Can I able convert the below string using Jackson or simple java 8 streams ?

public class test {

    public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        // TODO Auto-generated method stub
        String str = "[{empId=abc, empName=name, empSal=100, empEmail=test@gmail.com}, {empId=xyz, empName=test,abcexample, empSal=200, empEmail=test12@gmail.com}]";       
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        List<SearchDocument> arr = mapper.readValue(str, (new ArrayList<SearchDocument>()).getClass());
        System.out.println(arr);
    }

}

I tried the below approach, but a string has comma in value getting issue.

'String str = "[{empId=abc, empName=name, empSal=100, empEmail=test@gmail.com}, {empId=xyz, empName=test,abcexample, empSal=200, empEmail=test12@gmail.com}]";

    String elementType = StringUtils.substringBetween(str, "[", "]");
    String regex = "\\{|\\}|\\[|\\]";

    elementType = elementType.replaceAll(regex, " ");
    StringTokenizer token = new StringTokenizer(elementType, ",");
    while(token.hasMoreElements()){
        String str1 = token.nextToken();
        StringTokenizer furtherToken = new StringTokenizer(str1,"=");
        while(furtherToken.hasMoreTokens()){
            System.out.println("Key = "
                    + furtherToken.nextToken() + ", Value = " + furtherToken.nextToken());
        }
    }

'

  • Does this answer your question? [Is there a way to convert a String to a Java type using Jackson and/or one of its associated libraries (csv, json, etc.)](https://stackoverflow.com/questions/63757433/is-there-a-way-to-convert-a-string-to-a-java-type-using-jackson-and-or-one-of-it) – Ecstasy Jul 11 '22 at 04:55
  • I saw the above approach as said it is not valid JSON nor a CSV rows. However i tried using my approach and now edited the question with my approach but it has some issue. Need to find a way ignore comma in value to get parse properly any help! – Raj Kishore Jul 11 '22 at 15:40
  • @RajKishore You should get valid JSON when using the [Search REST API](https://learn.microsoft.com/en-us/rest/api/searchservice/preview-api/search-documents) directly. Can you share which request returned the string in question? Azure Cognitive Search also has a [Java SDK](https://learn.microsoft.com/en-us/java/api/overview/azure/search-documents-readme?view=azure-java-stable) so you don't need to deserialize it yourself. – giulianob Jul 11 '22 at 18:13
  • @giulianob : when i integrate with JAVA code it does not seems like getting as a JSON format. i am getting serachresult response a like below. {CreatedBy=XYZ, ModifiedDate=null, ModifiedBy=abc, Academic=degree, FirstName=test, empdetails = [{empId=abc, empName=name, empSal=100, empEmail=test@gmail.com}, {empId=xyz, empName=test,abcexample, empSal=200, empEmail=test12@gmail.com}]} Basically it is giving error while reading the nested rows, which is of type EDM.ComplexType , in above empdetails is the collection. if it is valid JSON i could have convert this with Jackson, but it is not. – Raj Kishore Jul 12 '22 at 06:27
  • Can you share sample code of how you are querying the service? The SDK supports deserializing to a POJO with `searchResult.getDocument(Hotel.class)` or to directly get fields using `searchResult.get("fieldName")`, see https://learn.microsoft.com/java/api/overview/azure/search-documents-readme?view=azure-java-stable#use-java-model-class-for-search-results – giulianob Jul 13 '22 at 18:18
  • I have gone through APIs and will able convert what ever I received from searchdocument. Thank you – Raj Kishore Jul 14 '22 at 19:19

0 Answers0