-2

I have an array of arrays such as this:

pl = [
        ["name1", "address1"],
        ["name2", ["address2"],
        ["name3", "address3"]
        ....
        ]

but I need to convert it into an array of objects:

pl = [
        {"name1": "address1"},
        {"name2": ["address2"},
        {"name3": "address3"}
        ....
        ]

I'm struggling, with no luck.

8
  • the first thing you post is already JSON. The second code you post is broken, there's an opening { but a closing ], and it's not clear what you want! Commented May 31, 2022 at 10:43
  • do you maybe mean "a dictionary mapping names to addresses" instead of "JSON"? Commented May 31, 2022 at 10:43
  • Neither input nor output is syntactically valid. If fixed, either could be represented as JSON. An array of heterogenous objects isn't an easy thing to work with, though. Commented May 31, 2022 at 10:44
  • @MarcusMüller, sorry! I have misplaced the braces. Now it is correct Commented May 31, 2022 at 11:03
  • 1
    Will this form help with your question? Commented May 31, 2022 at 11:38

1 Answer 1

-2

Docs: https://docs.python.org/3/library/json.html

Example from docs:

import json
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
Sign up to request clarification or add additional context in comments.

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.