0

i'm writing a python script in order to extract some data from a device and i need to formatting my json output. Now i have this output (example):

[
    [
        "Environment",
        "ok"
    ],
    [
        "Memory",
        "ok"
    ]
]

But i want to add labels in order to call values, like this example:

[
    [
        {"component":"Environment",
        "status":"ok"}
    ],
    [
        {"component":"Memory",
        "status":"ok"}
    ]
]

I'm trying to search online, but i cannot find what i'm searching. It is possibile?

Thank you.

2 Answers 2

2

Try this...

intermediate_output = [
    [
        "Environment",
        "ok"
    ],
    [
        "Memory",
        "ok"
    ]
]

final_output = [[{"component": x[0], "status": x[1]}] for x in intermediate_output]

print(final_output)

Output :

[[{'component': 'Environment', 'status': 'ok'}], [{'component': 'Memory', 'status': 'ok'}]]

EDIT :

Now if you want list of all components, before you move forward, I'd suggest you strip your nested list and make it a plain list.

>>> striped_list = [list1[0] for list1 in final_output]
>>> striped_list

[{'component': 'Environment', 'status': 'ok'}, {'component': 'Memory', 'status': 'ok'}]

Now, use list comprehension...

>>> list_of_components = [item['component'] for item in striped_list]
>>> list_of_components

['Environment', 'Memory']

For status of a particular component, again you could use for loops or list comprehensions.

>>> my_component = "Environment" # we have to find status of this component

>>> my_component_status = [item['status'] for item in striped_list if item['component'] == my_component]

>>> my_component_status

['ok']

Excellent documentation for understanding list comprehension

https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Cheers!!

Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot. it works! but i've two more question. Now my json is formatted like this: [ [ { "status": "ok", "component": "Environment" } ], [ { "status": "ok", "component": "Memory" } ] ] why status is the first element? If i want to have a list of all components, what's the command to launch? If i want to have the status of a single component, what's the command to launch? I ask because i've tried with some print commands without luck.
@maures before I could answer your questions, can you share the screenshot of the code where you're are getting status as the first element?
i've just a json array like that you've seen on my main question.
@maures I have run the same code multiple times and on different machines, and there is no way you would receive status as the first ellement,
that's not a problem. can you answer me about other question please?
|
0

Try this.

Using List comprehension.

a= [['Environment', 'ok'], ['Memory', 'ok']]
[{'component':x,'status':y} for x,y in a ]

output

[{'component': 'Environment', 'status': 'ok'},
 {'component': 'Memory', 'status': 'ok'}]

Comments

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.