Bobby
Bobby

Reputation: 189

How to Remove keyname from json string

I have a json string like this.

[
            {
                "_source": {
                    "name": "Jam Brong",
                    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
                    "price": "2500",
                    "slug": "133-jam-brong",
                    "short_name": "Jam Brong"
                }
            },
            {
                "_source": {
                    "name": "Jam abcfdfjn",
                    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
                    "price": "10888",
                    "slug": "87-jam-abcfdfjn",
                    "short_name": "Jam abcfdfjn"
                }
            }
]

I need to remove "_source":{

so i can get a json string like this.

[
            {

                    "name": "Jam Brong",
                    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
                    "price": "2500",
                    "slug": "133-jam-brong",
                    "short_name": "Jam Brong"

            },
            {

                    "name": "Jam abcfdfjn",
                    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
                    "price": "10888",
                    "slug": "87-jam-abcfdfjn",
                    "short_name": "Jam abcfdfjn"

            }
]

I tried to use replaceAll("("_source:{"),""); This code will show me some error like number expected.

I don't know how to use regex for the string which contains _ and { .

Before i think to use replaceAll, i tried jackson like this.

String responses ="";
        ObjectNode node = new ObjectMapper().readValue(response.toString(), ObjectNode.class);
        ProductList productListInstance = new ProductList();
        List<Product> productList = new ArrayList<>();

        try {
            if(node.get("hits").get("hits").isArray()){
                for (final JsonNode objNode : node.get("hits").get("hits")) {
                    Product products = new ObjectMapper().readValue(objNode.get("_source").toString(), Product.class);
                    productList.add(products);
                }
                productListInstance.setProductList(productList);
            }
            responses = productListInstance.toString();
        }
        catch (Exception ex){
            responses = productListInstance.toString();
        }
        return responses;

actually the first json string was like this :

{
    "hits": {
        "hits": [
            {
                "_source": {
                    "name": "Jam Brong",
                    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
                    "price": "2500",
                    "slug": "133-jam-brong",
                    "short_name": "Jam Brong"
                }
            },
            {
                "_source": {
                    "name": "Jam abcfdfjn",
                    "image": "https://asdf.sdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
                    "price": "10888",
                    "slug": "87-jam-abcfdfjn",
                    "short_name": "Jam abcfdfjn"
                }
            }
        ]
    }
}

Upvotes: 0

Views: 63

Answers (1)

priyanksriv
priyanksriv

Reputation: 1154

You can use Jackson to achieve this. First, define a bean representing your data model.

public static class Source {
    private String name;
    private String image;
    private String price;
    private String slug;
    private String shortName;

    @JsonCreator
    public Source(@JsonProperty("_source") Map<String, Object> rawJson) {
      this.name = rawJson.get("name").toString();
      this.image = rawJson.get("image").toString();
      this.price = rawJson.get("price").toString();
      this.slug = rawJson.get("slug").toString();
      this.shortName = rawJson.get("short_name").toString();
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getImage() {
      return image;
    }

    public void setImage(String image) {
      this.image = image;
    }

    public String getPrice() {
      return price;
    }

    public void setPrice(String price) {
      this.price = price;
    }

    public String getSlug() {
      return slug;
    }

    public void setSlug(String slug) {
      this.slug = slug;
    }

    public String getShortName() {
      return shortName;
    }

    public void setShortName(String shortName) {
      this.shortName = shortName;
    }
  }

Observe the @JsonCreator annotation on the constructor. Then write the code for serialization and deserialization:

    final ObjectMapper mapper = new ObjectMapper();
    Source[] sources = mapper.readValue(jsonStr, Source[].class);
    String converted = mapper.writeValueAsString(sources);
    System.out.println(converted);

Prints:

[
  {
    "name": "Jam Brong",
    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
    "price": "2500",
    "slug": "133-jam-brong",
    "shortName": "Jam Brong"
  },
  {
    "name": "Jam abcfdfjn",
    "image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
    "price": "10888",
    "slug": "87-jam-abcfdfjn",
    "shortName": "Jam abcfdfjn"
  }
]

Upvotes: 1

Related Questions