[Python] Dump JSON as multline String in Python with PyYaml

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I need to do a roundtrip with some Kubernetes Resources to alter it in some way with Python (3.11). Now i have t following ConfigMap-Resource:

    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: sample-map
    data:
    sample.json: |-
    [
    {
    "name": "foo",
    "description": "bar"
    }
    ]


    After loading it, altering it i want to dump it back, while preserving the former form. Unfortunately, i only get the following output:

    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: sample-map
    data:
    sample.json: "[\n {\n "name": "foo",\n"description": "bar"\n}\n]"


    I tried various things, but without any luck e.g with different representer combination as seen in https://github.com/yaml/pyyaml/issues/240 Does anyone have an idea how to do it? Thanks a lot!

    Continue reading...

Compartilhe esta Página