1

I have the following input: https://textuploader.com/dz3xo

This contains two orders.

The input can be converted into Json using:

print json.dumps(response2)

I want to manipulate this input to be shown as Json of like the following:

https://jsonblob.com/85f329dc-994c-11e8-8a91-931af4d591d9

The manipulation is simple: Any Sub-Json is removed and join to the father Json.

I wrote following functions:

def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + str(a) + '_')
        elif type(x) is list:
            out[name[:-1]] = x
        else:
            out[name[:-1]] = x

    flatten(y)
    return out


def generatejson2(response2):
    sample_object = pd.DataFrame(response2).to_dict()
    flat = {k: flatten_json(v) for k, v in sample_object.items()}
    return json.dumps(flat, sort_keys=True)


response2 = Func_Create_Data()
flat_json = generatejson2(response2)

However this is not my desired out put it. It gives: enter image description here

It mix data from the two orders under the same index. The row numbers should never be in the index name.

I can't find the problem with my code. Any idea what's wrong?

1 Answer 1

1

Your flatten function should be working right, are you sure you're handing it the right information in the line

flat = {k: flatten_json(v) for k, v in sample_object.items()}

it looks a lot like the 'k' in that line is ['customer', 'shippingAddress', ...] rather than the expected ['0', '1']. My guess is you should have a

0_country_currency_id: 2

inside your shippingAddress?

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

6 Comments

nop.. The first line in my question is the source input you can see there is no such value in the shipping address.. the 0 and 1 comes from the row numbers...
I meant inside the shippingAddress {62} line in the pic close to the bottom of your post, not in the original. and yeah, that's where the 0 and 1 come from, but I think those and the highest-level keys in the dict are getting swapped somewhere.
The pic is the output of the code which is wrong. The desired output is: jsonblob.com/85f329dc-994c-11e8-8a91-931af4d591d9 where can they be swapped?
I'm not sure, but it's most likely in the first two lines of generatejson2. One of those lines is probably not giving the result you think it should be. The flatten_json function works, using it on just the contents of one order I get jsonblob.com/de772af9-995d-11e8-8a91-f3b26d260dd6, which matches yours, so the only possibility is that it was handed data in a wrong format.
It think it's most likely 'pd.DataFrame(response2).to_dict()', you should take a look at what that's actually giving you.
|

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.