<p>I want to convert this file into json format, does anyone know how to do it?</p>
<p>This is the yaml file :</p>
<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: theiaide
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  #name: rewrite
  name: theiaide
  namespace: theiaide
  annotations:
    #nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: ide.quantum.com
    http:
      paths:
      - path: /
        backend:
          serviceName: theiaide
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
 name: theiaide
 namespace: theiaide
spec:
 ports:
 - port: 80
   targetPort: 3000
 selector:
   app: theiaide
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: theiaide
  name: theiaide
  namespace: theiaide
spec:
  selector:
    matchLabels:
      app: theiaide
  replicas: 1
  template:
    metadata:
      labels:
        app: theiaide
    spec:
      containers:
      - image: theiaide/theia-python
        imagePullPolicy: IfNotPresent
        name: theiaide
        ports:
        - containerPort: 3000

</code></pre>
<p><strong>code.py</strong></p>
<pre><code>import json,yaml
txt=&quot;&quot;
with open(r&quot;C:\Users\77922\PycharmProjects\ide-ingress.yaml&quot;,'r') as f:
    for a in f.readlines():
        txt=txt+a
print(yaml.dump(yaml.load_all(txt),default_flow_style=False))
print(json.dumps(yaml.load_all(txt),indent=2,sort_keys=True))
</code></pre>
<p>when I run python code.py ,and I got the error:</p>
<pre><code>TypeError: can't pickle generator objects
</code></pre>
<p>I don’t know if it is the reason for this --- delimiter, because there are multiple --- delimiters in my yaml file</p>
<p>Then I tried the following function:</p>
<pre><code>def main():

    # config.load_kube_config()

    f = open(r&quot;C:\Users\77922\PycharmProjects\ide-ingress.yaml&quot;,&quot;r&quot;)
    generate_dict  = yaml.load_all(f,Loader=yaml.FullLoader)
    generate_json = json.dumps(generate_dict)
    print(generate_json)
    # dep = yaml.load_all(f)
    # k8s_apps_v1 = client.AppsV1Api()
    # resp = k8s_apps_v1.create_namespaced_deployment(
    #     body=dep, namespace=&quot;default&quot;)
    # print(&quot;Deployment created. status='%s'&quot; % resp.metadata.name)
if __name__ == '__main__':
    main()

</code></pre>
<pre><code> raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type generator is not JSON serializable
</code></pre>
<p>I just want to use this yaml file to call kubernetes api to generate namespace</p>

Layne Wu
<p>Your file contains more than one document. You should use the <code>safe_load_all</code> function rather than <code>yaml.load</code> and <code>list</code> rather than <code>json.dumps</code></p>
<pre><code>import json,yaml
txt=&quot;&quot;
with open(r&quot;C:\Users\77922\PycharmProjects\ide-ingress.yaml&quot;,'r') as f:
    for a in f.readlines():
        txt=txt+a
print(yaml.dump_all(yaml.safe_load_all(txt),default_flow_style=False))
print(list(yaml.safe_load_all(txt)))
</code></pre>
