1

I am sending JSON data over post method to one of my ML models, the problem is it is not able to pick which object to fetch for fields.

Below is the sample JSON

 {
    "a": {
        "asD": 1553990400000,
        "cust": "S65A00",
        "party": "1234",
        "custS": null,
        "sngldt: 1557014400000,
},
 "b": {
        "History": [],
        "cust": "S65A00",
        "mb_cntry": "US",
        "mbdt": 1490918400000,
        "mbsg_dt": 1553904000000,
}
}

How Can I Merge this JSON in the ML Code in single braces like below, I don't have the luxury to format the JSON itself during Post request.

{

        "asD": 1553990400000,
        "cust": "S65A00",
        "party": "1234",
        "custS": null,
        "sngldt: 1557014400000,
        "History": [],
        "mb_cntry": "US",
        "mbdt": 1490918400000,
        "mbsg_dt": 1553904000000,

}

Below is the code that I have tried but failing

@app.route('/', methods=['GET', 'POST'])
def execute():
    if request.method == 'POST':
        json_data = request.get_json()
        batch=json.dumps(json_data)
        dataFrame = pd.DataFrame(json_data)
        print(len(dataFrame.columns))
        df=pd.melt(dataFrame,id_vars=[' ','b'],  value_name='values')
        print(df)

2 Answers 2

2
merged = dict()
merged.update(obj.a)
merged.update(obj.b)
Sign up to request clarification or add additional context in comments.

Comments

1

With unpacking syntax:

...
json_data = dict(json_data['a'], **json_data['b'])
print(json_data)

prints:

{'asD': 1553990400000, 'cust': 'S65A00', 'party': '1234', 'custS': 'null', 'sngldt': 1557014400000, 'History': [], 'mb_cntry': 'US', 'mbdt': 1490918400000, 'mbsg_dt': 1553904000000}

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.