4

How to read json as a string from a yaml file in spring boot application ?

I need to get the json as a string / json object.

Application.yml

Shirt:
 rest:
  booking:
   invoiceAddress: {"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}

Configuration class

@Configuration
public class ConfigurationServiceClientConfiguration {

    @Value("${Shirt.rest.booking.invoiceAddress}")
    private String invoiceAddress;

    @Bean
    public String getInvoiceAddress(){
        System.out.println(invoiceAddress);
        return this.invoiceAddress;
    }
}

Also tried @JsonProperty but no idea whether my usage was wrong or not !

Note: I am not ready to change the json as a list because more jsons (of invoiceAddress) to come and I will have to make list for each. So it's easy for me if the json can be copied as it is to the yaml file.

smilyface
  • 5,021
  • 8
  • 41
  • 57
  • http://stackoverflow.com/questions/9532840/embedding-json-data-into-yaml-file/9532944#9532944 – buræquete Apr 13 '17 at 05:36
  • you haven't posted your problem/error! Kindly post the `stack trace` or `error log` – Arjun Apr 13 '17 at 05:37
  • 1
    Possible duplicate of [Embedding JSON Data into YAML file](http://stackoverflow.com/questions/9532840/embedding-json-data-into-yaml-file) – buræquete Apr 13 '17 at 05:38

4 Answers4

12

If you want as a plan string you should add this value between single quote 'your value.' in your yml file

Shirt:
 rest:
  booking:
   invoiceAddress: '{"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}'
Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
0

Try modifying your yml file as below:

Shirt.rest.booking:
   invoiceAddress: "\"name\":\"VIA Pics AB\",\"address1\":\"VIA Pics AB\",\"address2\":\"Mejerivägen 3\",\"zipCode\":\"11743\",\"city\":\"STOCKHOLM\",\"countryCode\":\"SE\",\"email\":\"w.ho@Pics.fr\""
0

Single quotes escapes all the special characters in the values of YAML properties. Just try adding to the string into single quotes 'PropertiesValue'.

E.g :

invoiceAddress: '{"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}'
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Nilesh Kumar
  • 109
  • 1
  • 5
0
Shirt:
 rest:
  booking:
   invoiceAddress: >
    {"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}

@see https://stackoverflow.com/a/17093553/2929352

David KELLER
  • 464
  • 4
  • 8