0

I have a list which is like

DecomposeTables = ['{ attr:["C", "A", "B"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:13, }', '{ attr:["C", "A", "B"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:13, }', '{ attr:["C", "A", "B"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:13, }']

i want to send it as json for api response i have tried

jsonpickle.encode(DecomposeTables,unpicklable=False)

but on front end i am reciving only one object as json other are null

data: 
["{ attr:["C", "B", "A"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:6, }", "{ attr:["C", "B", "A"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:6, }", "{ attr:["C", "B", "A"],fds:[[["A"], ["A"]], [["C"], ["B"]]],normalForm:"BCNF",pid:1,id:6, }"]
4
  • How much control do you have over DecomposeTables? Things would be a lot easier if the keys were in quotes, viz. '{ "attr": ["C", "A", "B"], "fds": ... }' Commented Jun 15, 2020 at 9:57
  • i can change that ,what shoud i do after that If i have the above format as you say @mrblewog Commented Jun 15, 2020 at 10:14
  • See answer -- and the caveat... Commented Jun 15, 2020 at 10:20
  • If you found the answer useful -- stackoverflow.com/help/someone-answers Commented Jun 19, 2020 at 7:56

1 Answer 1

1

Picking up from the comments.... assuming that you can put the keys in quotes.

(I'm using Python 3.8.1)

import ast
import json

# Note "attr" not attr, and so on.
# Each string is actually a string representation of a Python dictionary.
DecomposeTables = [
    '{ "attr":["C", "A", "B"],"fds":[[["A"], ["A"]], [["C"], ["B"]]],"normalForm":"BCNF","pid":1,"id":13 }', 
    '{ "attr":["C", "A", "B"],"fds":[[["A"], ["A"]], [["C"], ["B"]]],"normalForm":"BCNF","pid":1,"id":13 }', 
    '{ "attr":["C", "A", "B"],"fds":[[["A"], ["A"]], [["C"], ["B"]]],"normalForm":"BCNF","pid":1,"id":13 }'
    ]

# https://docs.python.org/3/library/ast.html => safely evaluate each string as a Python dictionary
as_dicts = [ast.literal_eval(x) for x in DecomposeTables]

# Now as_dicts is a Python list with Python dictionaries in it.
# You might be able to just send that object with Flask,
# but as an example here I'm writing the list as a json string
as_json = json.dumps(as_dicts)
print(as_json)

gives

[{"attr": ["C", "A", "B"], "fds": [[["A"], ["A"]], [["C"], ["B"]]], "normalForm": "BCNF", "pid": 1, "id": 13}, {"attr": ["C", "A", "B"], "fds": [[["A"], ["A"]], [["C"], ["B"]]], "normalForm": "BCNF", "pid": 1, "id": 13}, {"attr": ["C", "A", "B"], "fds": [[["A"], ["A"]], [["C"], ["B"]]], "normalForm": "BCNF", "pid": 1, "id": 13}]
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.