0

i am trying to place a JSON entry in yaml . but its not compiling . My expected yaml file is like below .

http:
    port: "8081"
ABC:          
    CustomFieldJson: "[
                      {
                     "FieldName": "uw_firm",
                     "FieldValue" :"NULL" 
                      },
                      {
                      "FieldName": "uw_type",
                      "FieldValue" :"Delegated"
                      }
                     ]"

How i can fix this?

VKP
  • 589
  • 1
  • 8
  • 34
  • 1
    escape the double quotes. http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html Escape sequences work in YAML for double quoted strings – Khanna111 Feb 13 '21 at 04:27
  • also see https://stackoverflow.com/questions/9532840/embedding-json-data-into-yaml-file – wisbucky Apr 26 '23 at 18:05

2 Answers2

1

The solution is to escape the double quotes in the json value. Escape sequences are acceptable in YAML when using double quotes for the scalar value. And this scalar value could be json as in your case.

An example will make it clear:

http:
    port: "8081"
ABC:          
    CustomFieldJson: "[
                      {
                     \"FieldName\": \"uw_firm\",
                     \"FieldValue\" :\"NULL\" 
                      }
                     ]"

This page has useful information.

Khanna111
  • 3,627
  • 1
  • 23
  • 25
1

The easiest way in this case is to use a "literal block scalar". This way you don't have to escape anything.

The literal block scalar is introduced with a | pipe sign.

http:
    port: "8081"
ABC:          
    CustomFieldJson: |
                      [
                      {
                     "FieldName": "uw_firm",
                     "FieldValue" :"NULL" 
                      },
                      {
                      "FieldName": "uw_type",
                      "FieldValue" :"Delegated"
                      }
                     ]

(More about quoting in YAML)

tinita
  • 3,987
  • 1
  • 21
  • 23