0

Let's say I have 2 JSON objects (dictionaries):

first_dict={"features": [{"attributes": {"id": "KA88457","name": "Steven", "phone":"+6590876589"}}]}
second_dict={"features": [{"attributes": {"id": "KA88333","name": "John", "phone":"+6590723456"}}]}

I want to add them so that I have something like this:

{"features": [{"attributes": {"id": "KA88457","name": "Steven", "phone":"+6590876589"}}], 'features': [{'attributes': {'id': 'KA88333', 'name': 'John', 'phone': '+6590723456'}}]}

If I use first_dict.update(second_dict), I get the following. How do I fix that?

{'features': [{'attributes': {'id': 'KA88333', 'name': 'John', 'phone': '+6590723456'}}]}
1
  • Your desired result describes a dictionary that maps a single key to two distinct values, which is impossible. Could you elaborate on your usecase some more? Do you want to be able to lookup the name+phone number by id? Commented Aug 22, 2018 at 19:32

2 Answers 2

1

Since both dicts have the same key, "features", you'll need to rename one of the keys and add it and its values into one of the dictionaries. This is one way to avoid your merge conflict. E.g.:

second_dict={"features": [{"attributes": {"id": "KA88333","name": "John", "phone":"+6590723456"}}]}
first_dict={"features": [{"attributes": {"id": "KA88457","name": "Steven", "phone":"+6590876589"}}]}

temp_var = second_dict['features']
first_dict['features2'] = temp_var

merged data in first_dict:

{'features': [{'attributes': {'id': 'KA88457', 'name': 'Steven', 'phone': '+6590876589'}}], 'features2': [{'attributes': {'id': 'KA88333', 'name': 'John', 'phone': '+6590723456'}}]}
Sign up to request clarification or add additional context in comments.

1 Comment

This works well, although I need to actually look closer at my data. This is a very simplified version of what I am actually pulling back (1000's of features) but I think I may be able to apply it this way.
1

According to RFC 7159, You cannot have duplicate names inside objects.

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

Although, The original JSON standard ECMA-404 doesn't say anything about duplicate names. Most of JSON libraries (including python3 JSON library) doesn't support this feature.

Another reason that you can't do this is you're trying to have two different values for a key in your dictionary (which is basically a hash map).

If you really need this you have to write your own serializer or maybe find a JSON library for python that supports this.

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.