0

I want to loop through the json object and add the json values to dictionary and finally append that dictionary to list. For example:-

lists=[]
dicts={}
for objects in json:
   dicts["a"]= objects["abc"]
   dicts["b"] = objects["xyz"]
   lists.append(dicts)

Input data json:-

{ "json" : [ { 'abc'= 'string1', 'xyz='string2', 'a': 'name'}, { 'abc'= 'john', 'xyz='joe', 'a': 'name'},{ 'abc'= 'b', 'xyz='c', 'a': 'name'} ]}

Expectation for the output is list inside the dictionary like this:-

[{'a': 'string1','b': 'string2'}, {'a': 'john','b': 'joe'}]

How can i achieve this? Any help much appreciated!

2
  • are you getting any error for the code that you tried? Commented Jun 6, 2019 at 8:10
  • Not an error but i am getting the same output every time, so my output is :-[{'a': 'string1','b': 'string2'}, {'a': 'string1','b': 'string2}]. So actually the values are updating in dictionary for same key. Commented Jun 6, 2019 at 8:13

1 Answer 1

1

Using dicts you are over-writing with the last value in json instead

Use:

results = []
for objects in json:
    results.append({"a": objects["abc"], "b": objects["xyz"]})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much:) Its so easy if i think little much:).Thanks for the help.

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.