-2

I need to figure out how to structure my data in python such that when I call dumps and write to file I get the following structure of data as an example:

{
    "Prop_a": [
    {
        "car": "brown",
        "color": "yellow",
        "engine": [
        {
            "mod_a": "x1",
            "name": [
            {
                "diesel": "yes",
            }
...

As you can see, I have nested elements that need expanded. The end-goal is to import the data into a database, I need JSON or CSV formatted data to do it.

EDIT: To all: I can easily print a single level of the dict of lists to JSON. What I need assistance with is how to format the nested structure.

EDIT #2:
Since code is being requested...

my_dict = {}
for x in group:
   my_dict[x] = []
   for y in sub_group:
       mydict[x].append(data_symbol_reference)

Produces an output like:

{
  "Prop_a" : [
      "car",
      "color",
   ],
...

I need assistance on the nesting the dict of lists within the list structure.

Shawn
  • 353
  • 1
  • 14
  • 1
    if you already have a dict of lists, you can dump it as is – Jean-François Fabre May 05 '18 at 12:52
  • 1
    Possible duplicate of [Python dump dict to json file](https://stackoverflow.com/questions/17043860/python-dump-dict-to-json-file) – DollarAkshay May 05 '18 at 12:53
  • 1
    You need to provide your input, the desired output and your attempt to make it happen. – khachik May 05 '18 at 12:53
  • @Jean-FrançoisFabre I do have a dict of lists but it is more than that because of the nested structure. Its a dict of lists with dicts of lists inside. Or at least thats how I think i need the data structured? – Shawn May 05 '18 at 12:55
  • @khachik The structure of the input is basically what I'm trying to figure out. The output is above and the attempt to make it happen has been a set of failures. – Shawn May 05 '18 at 12:58
  • So show us your code (with relevant sample input) and we can help you fix it. Otherwise your question may be closed as unclear until you more clearly explain the actual problem you're having. – PM 2Ring May 05 '18 at 12:59
  • It's difficult to represent nested data-structures in CSV format, so I suggest you concentrate on JSON. What you have looks like it could be converted very easily using the `json` module's `dump()` command. – martineau May 05 '18 at 13:05
  • All, I have updated my response with code and current output. – Shawn May 05 '18 at 13:09
  • @martineau Agreed. I've been focusing on representing the relationships in JSON format – Shawn May 05 '18 at 13:15
  • Thanks for adding some code, but it's still not clear what your actual problem is. What is `data_symbol_reference`, and why are you appending it repeatedly? The latest output you posted looks strange: it's got square brackets instead of braces around `"car": "brown", "color": "yellow",`. Don't try to produce JSON output by hand, use the JSON module. – PM 2Ring May 05 '18 at 13:37
  • @PM2Ring Yes, it has square brackets around the inner portion because the dictionary is composed of lists. That's just how the JSON module displayed the dictionary data structure. I just noticed I had an extra piece in my bottom "current output" code block. there isn't a key/value pair within the list yet (though that is desired). I think I may need to ask this question again more clearly to get assistance. – Shawn May 05 '18 at 13:41
  • Ok, the new version of the output makes sense now. :) I'd love to write an answer for you, but I don't want to write code that doesn't actually do what you need. I take it you want the output to be like what's at the top of your question, but we need to see what your input data really looks like so we can show you how to get it into that form. – PM 2Ring May 05 '18 at 13:50
  • Sadly, the input data has to be "teased out" of the API of an application in such a way that it would likely add more confusion than it would help. – Shawn May 05 '18 at 13:53
  • The trick was to nest with setdefaults. data.setdefault(str(props), {}).setdefault(str(engines), {})[index] = string_value – Shawn May 05 '18 at 21:58

1 Answers1

-4

Get the official docs for python 3.6: JSON encoder and decoder

RomanS
  • 196
  • 1
  • 3
  • 1
    sorry if I do not made your point but rather than to be that offensive if people wants to help, you should more clarify your questions and guide me/community to the right way if I/we missed something, that's the meaning of understanding each other – RomanS May 05 '18 at 13:53