python icon indicating copy to clipboard operation
python copied to clipboard

Transforming API objects in JSON

Open lainra opened this issue 9 years ago • 5 comments

Hello,

When I retrieve objects from the API, (i.e api.list_namespaced_config_map(namespace)), I get an output not JSON serializable because all keys are single quoted instead of double quoted:

{'api_version': 'v1', 'items': [{'api_version': None, 'data': {} 'kind': None, 'metadata': {'annotations': None, 'cluster_name': None, 'uid': 'c57fbrtrtrtrttrrtr60b9f3cc7b9'}}], 'kind': 'ConfigMapList', 'metadata': {'resource_version': '287723763', 'self_link': '/api/v1/namespaces/blabla'}}

Is it an intended behaviour? In that case, how could I process API objects to be used and manipulated as JSON? There is also an issue with None values not double quoted. I'm sure it is a newbie question but I don't know what to do.

Thank you for your help.

lainra avatar Nov 14 '17 06:11 lainra

See #390, and you can try using json.dump for serializing to JSON.

allanadair avatar Nov 16 '17 10:11 allanadair

Hi @allanadair,

Thank you very much! This works like a charm!

I would never have been able to find it by myself...

Sincerely,

Lain92

lainra avatar Nov 17 '17 07:11 lainra

I tried doing a json.dumps() but got this

abc=api3.list_ingress_for_all_namespaces()
jabc = json.dumps(abc)
TypeError: Object of type 'ExtensionsV1beta1IngressList' is not JSON serializable

kartik-moolya avatar Aug 26 '20 16:08 kartik-moolya

I tried doing json.dumps() but I got this v1 = client.CoreV1Api() ret = v1.list_node() nodelist = json.dumps(ret)

TypeError: Object of type 'V1Node' is not JSON serializable

harshalcse avatar Dec 03 '20 04:12 harshalcse

Hey Take a look at this comment. preload_content is probably what you want there.

moshevayner avatar Dec 03 '20 13:12 moshevayner