0

I have 2 json files! f1.json and f2.json

Contents: "f1.json"

{
  "tests":
    [
      {"a": "one", "b": "two"},
      {"a": "one", "b": "two"}
    ]
}

Contents: "f2.json"

{
  "tests":
    [
      {"c": "three", "d": "four"}
    ]
}

Required output - in list format

[{a:"one",b:"two"},{a:"one",b:"two"},{c:"three",d:"four"}]

I'm getting the same output in the "unicode" format. Does anyone have a way to get it without the unicode?

My output

[{u'a': u'one', u'b': u'two'}, {u'a': u'one', u'b': u'two'}, {u'c': u'three', u'd': u'four'}]

Code:

files=['t1.json','t2.json']      


import json,ast                          

empty = []                             


for elements in files:           
    fh = open(elements, 'r')             
    filedata = fh.read()                 
    fh.close()                           
    data = json.loads(filedata)          

    empty.append(data['tests'])        
final = []                            
for elements in empty:                 
    for dict in elements:                
        final.append(dict)            
print final                          
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Arohi Gupta
  • 95
  • 1
  • 8

2 Answers2

0

You could get string object instead of Unicode ones from JSON.

Refer to Mark Amery's function and How to get string objects instead of Unicode ones from JSON in Python?

So you can convert the Unicode data(variable) to string object in your code after the line:

data = json.loads(filedata)
data = byteify(data)    # Add this line according to Mark Amery's function
zhenguoli
  • 2,268
  • 1
  • 14
  • 31
0

try below code

import json

l = []
files=['t1.json','t2.json']
for file in files:
   with open(file, 'r') as file:
       d = json.loads(file.read())
       l.extend(d["tests"])
print l
anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62