0

I have been trying to format live JSON data coming from an API to be read in Django views. However, data coming is little complicated.

I have incoming JSON data in format

    { Time1:
         {'A':'Value',
          'B':'Value',
          }
     Time2:
         {'A':'Value',
          'B':'Value',
          }
there are multiple time records....
}

I need to convert it into

{
  'Time': Time1
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time2
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time3
  'A'   : 'Value'
  'B'   : 'Value'
},
{
  'Time': Time4
  'A'   : 'Value'
  'B'   : 'Value'
},...and so on
  • 1
    what have you tried so far? this seems similar to [this type of question](https://stackoverflow.com/questions/6027558/flatten-nested-python-dictionaries-compressing-keys). Although not the same, it seems it may be very similar – MattR Feb 22 '18 at 14:26
  • It's not json. There's no list `[...]` that wraps all the dictionaries, no commas between elements etc. – kszl Feb 22 '18 at 14:29
  • Python 2.7 or Python3+ ? – Stphane Feb 22 '18 at 14:39
  • @MattR This is certainly not i desire. currently, i am trying to initialize an new dict with just time values and then later create a another new dict {dict1+existing dict } – shivam singhal Feb 22 '18 at 14:56
  • @Stphane python 3+ – shivam singhal Feb 22 '18 at 14:57
  • @shivamsinghal I have posted an answer according to the Python branch you use. – Stphane Feb 22 '18 at 15:06

3 Answers3

0

Assuming your data is in a proper dict format incoming. You can do this

in_json = { 2014: {'A':'Value', 'B':'Value'}, 2015: {'A':'Value', 'B':'Value'}}
newl = list()

for k in in_json:
    x = dict({'Time':k})
    x.update(in_json[2014])
    newl.append(x)

Basically just adding the key from the original dict as a value to a dict and appending it to the list

usernamenotfound
  • 1,540
  • 2
  • 11
  • 18
0

As mentioned in comments, indicated output format is a list. In this case you'll just add the new entry for "Time" for each nested dict:

final_list = []
 for key, subdict in in_dict.iteritems():
     subdict["Time"] = key 
     final_list.append(subdict)

Or if you prefer inline:

final_list = [dict([("Time", key)] + sub.items()) for key, sub in in_dict.iteritems()]
ibt23sec5
  • 369
  • 3
  • 13
0

You can use a list comprehension structure, (the code below has been tested using Python3.6):

# Given your JSON has successfully been parsed into a dictionary
> input={'Time1': {'A':'Value1A', 'B':'Value1B'}, 'Time2': {'A','Value2A', 'B', 'Value2B'}}

# Iterate over the dictionary and build a new item out of each key/value couple
> transformed=[(v.update({'Time': k}) or v) for (k, v) in input.items()]

> print(transformed)
[
  {
    'A'   : 'Value1A'
    'B'   : 'Value1B'
    'Time': 'Time1'
  },{
    'A'   : 'Value2A'
    'B'   : 'Value2B'
    'Time': 'Time2'
  }, …
]

What happens with (v.update({'Time': k}) or v) ?
Given v is a dictionary, v.update(...) will add a new entry to the instance (in other words, it mutates the instance). This method doesn't return the instance though but None, fortunately wrapping the call between parenthesis gives the opportunity to build an expression that will ultimately return the v instance. v being a dict object (given it is not empty) it will be evaluated truthfully in a Boolean context, hence the (… or v) construct.

Stphane
  • 3,368
  • 5
  • 32
  • 47
  • This was really helpful. – shivam singhal Feb 22 '18 at 15:29
  • Useful answer on merging dictonaries in case you need it: [merge-two-dictionaries-in-a-single-expression](https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression#answer-26853961) – Stphane Feb 22 '18 at 15:34
  • @shivamsinghal please consider set your question as solved by ticking the answer that helped you the most. – Stphane Feb 24 '18 at 13:08