lokesh
lokesh

Reputation: 49

How to compare json response with XML vales?

I'm getting a JSON response for my API. Now I need to compare all the nodes present in the JSON response with my XLM file.

Example:

Json response:

> {fields=[{field_id=UDF_CHAR1, is_visible=true, default_value=null,
> field_type=single_line, field_name=build name}

XML file:

<template name="fields">
    <key name="field_id" type="String" />
    <key name="is_visible" type="Boolean" />
    <key name="default_value" type="String_or_null" />
    <key name="field_type" type="String" />
    <key name="field_name" type="String" />
</template>

How can i compare all the key values in my xml file with all the nodes in the JSON response.

Upvotes: 1

Views: 2420

Answers (2)

Suresh
Suresh

Reputation: 64

Please follow these steps

  1. Create a class(may be a modal class) that can keep data of the XML file and JSON response. Eg:

    class Fields { String fieldId; boolean isVisible; }

  2. Add a method(function) to compare two instance of above class. Eg: public boolean isEqualFields(Fields anotherField);

  3. Parse your JSON response and create an instance of above class.
  4. Also create an instance of above class from XML.
  5. Now you can use "isEqualFields" function to compare instance from JSON and instance from XML

Upvotes: 0

niemar
niemar

Reputation: 642

  1. Create Java POJO that describes Fileds.
  2. Use for example Jackson or other library to convert json to Fileds object.
  3. Use for example Jackson or Jaxb to convert xml to Fileds.
  4. Implement equals and hashcode methods for Fileds.
  5. Compare objects.

Upvotes: 3

Related Questions