3

I want to convert this file into json format, does anyone know how to do it?

This is the yaml file :

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.py

import json,yaml
txt=""
with open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml",'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))

when I run python code.py ,and I got the error:

TypeError: can't pickle generator objects

I don’t know if it is the reason for this --- delimiter, because there are multiple --- delimiters in my yaml file

Then I tried the following function:

def main():

    # config.load_kube_config()

    f = open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml","r")
    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="default")
    # print("Deployment created. status='%s'" % resp.metadata.name)
if __name__ == '__main__':
    main()

 raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type generator is not JSON serializable

I just want to use this yaml file to call kubernetes api to generate namespace

4
  • Why do you need to dump it to JSON? If you already have that YAML, using subprocess.call(['kubectl', 'apply', '-f', 'ide-ingress.yaml']) could load it, or as you show in the last example, the Kubernetes Python client has native data structures and APIs for Kubernetes objects. Commented Jul 23, 2020 at 10:24
  • @Layne Wu is the solution posted by Ewerton solved your issue? Commented Jul 23, 2020 at 15:37
  • @DavidMaze In fact, I just want to use the API to call, but currently create deployment fails. The creation of igress namespace and service is successful. Commented Jul 24, 2020 at 2:57
  • @DawidKruk Yes, Ewerton helped me solve this file conversion problem, but I seem to be discussing something else with David Maze. Do I need to open a new question to discuss it? Commented Jul 24, 2020 at 3:00

1 Answer 1

3

Your file contains more than one document. You should use the safe_load_all function rather than yaml.load and list rather than json.dumps

import json,yaml
txt=""
with open(r"C:\Users\77922\PycharmProjects\ide-ingress.yaml",'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)))
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it and it seems that there were other errors. I updated it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.