1

I want to create json data which exactly looks like this:

[
      {
        "mainDetail": {
            "number": "12345",    
            "inStock": 111
        }
      },
      {
        "mainDetail": {
            "number": "54321",
            "inStock": 222
        }
      }
]

The data "number" and "instock" are read from python dict:

articleDict = {}
d=[{'number': x, 'inStock': y} for x,y in articleDict.items()]
print json.dumps(d, indent=4)

This is what i get so far:

[
    {
        "number": "12345",
        "inStock": 111
    },
    {
        "number": "54321",
        "inStock": 222
    }
]

I am just missing the "mainDetail", I'm at an impasse with my ideas. What can I do to make it work?

1
  • 1
    d=[{'mainDetail': {'number': x, 'inStock': y}} for x,y in articleDict.items()]? Commented Jan 22, 2020 at 13:51

1 Answer 1

2
d=[{"mainDetail" : {'number': x, 'inStock': y}} for x,y in articleDict.items()]

You need to add this "mainDetail" when creating your list.

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

1 Comment

Thank you very much. This is the expected result.

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.