0

The following code was used to create JSON. But I have \ in JSON.

"[{\"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"1_245_183_424_225_251_188_416_219_SJA4549J.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"plate\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}, {\"IMG_20200214_081450.jpeg\": {\"fileref\": \"\", \"size\": 0, \"filename\": \"IMG_20200214_081450.jpeg\", \"file_attributes\": {}, \"regions\": {\"textline\": {\"shape_attributes\": {\"name\": \"polygon\", \"all_points_x\": [], \"all_points_y\": []}, \"region_attributes\": {\"width\": 720, \"height\": 720, \"depth\": 3}}}}}]"

Then I also have "[ at the beginning and ]" at end of file in printing to JSON file. Why I have those at the beginning and end of JSON file and how to remove?

The code is as follow.

import os
import xml.etree.ElementTree as ET
import pickle
from os import listdir, getcwd
from os.path import join
import numpy as np
import cv2
import math
import random
import json
xmlspath="/home/itc/Data/NumberPlate/PlateDetection/annotations/xmls/"


xmlFiles = [f for f in listdir(xmlspath) if f.endswith("." + "xml")];
num_objs = 0
iou = 0.0
num_hits = 0
train = []
val = []
for idx,xmlFile in enumerate(xmlFiles):
   if(idx > 10):
      break
   tree = ET.parse(xmlspath+xmlFile)
   root = tree.getroot()
   filename = xmlFile.split(".")[0]+".jpeg"
   size = root.find("size")
   width = int(size.find("width").text)
   height = int(size.find("height").text)
   depth = int(size.find("depth").text)
   for obj in root.iter("object"):
      newitem={
  "name":{"fileref":"","size":0,"filename":"","file_attributes":{},"regions":{"attribute":{"shape_attributes":{"name":"polygon","all_points_x":[],"all_points_y":[]},"region_attributes":{"width":0,"height":0,"depth":3}}}}  
}
      newitem[filename]=newitem.pop("name")
      newitem[filename]["filename"]=filename
      bndbox = obj.find("bndbox")
      category = obj.find("name")
      xmin=int(bndbox.find("xmin").text)
      ymin=int(bndbox.find("ymin").text)
      xmax=int(bndbox.find("xmax").text)
      ymax=int(bndbox.find("ymax").text)
      if(obj.find("name").text == "plate"):
         newitem[filename]["regions"]["plate"] = newitem[filename]["regions"].pop("attribute")
         newitem[filename]["regions"]["plate"]["region_attributes"]["width"]=width
         newitem[filename]["regions"]["plate"]["region_attributes"]["height"]=height  
      elif(obj.find("name").text == "textline"):
         newitem[filename]["regions"]["textline"] = newitem[filename]["regions"].pop("attribute")
         newitem[filename]["regions"]["textline"]["region_attributes"]["width"]=width
         newitem[filename]["regions"]["textline"]["region_attributes"]["height"]=height                  
      if(idx%8 == 0):
         val.append(newitem)
      else:
         train.append(newitem)
      print(idx)
      

jsontrain = json.dumps(train)
jsonval = json.dumps(val)
with open("numplate/train/train.json", "w") as outfile:
    json.dump(jsontrain, outfile)
with open("numplate/val/val.json", "w") as outfile:
    json.dump(jsonval, outfile)
      
      
6
  • 1
    You're JSON-encoding your JSON string. Don't both dump and dumps the value. Commented Oct 28, 2020 at 8:03
  • Cant you mininize a little? It seems like you are serializing twice... Commented Oct 28, 2020 at 8:03
  • And you have a [] because you're creating and dumping a list. Commented Oct 28, 2020 at 8:03
  • @deceze thanks I just need dump to JSON file. Commented Oct 28, 2020 at 8:08
  • 1
    Don't create a list. I don't know what else you'd want, since you explicitly declare those values as [] and append to it in a loop… Commented Oct 28, 2020 at 8:10

1 Answer 1

1

The reason that you have "[ at the beginning and ]" at end of file" is that because your objects (val an train) are lists (not dictionaries)

First you have to convert them:

train = {"items" : train}
val =  = {"items" : val}

Then, for your encoding issue you can force the json.dump() function to not add the "" symbol by setting the ensure_ascii to False:

with open("numplate/train/train.json", 'w', encoding='utf8') as outfile:
    json.dump(train, outfile, ensure_ascii=False)
Sign up to request clarification or add additional context in comments.

3 Comments

"The "\" symbol" has nothing to do with ensure_ascii.
docs.python.org/2/library/json.html It is mentioned in the official doc that " If ensure_ascii is true (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only."
Yeah, that just has nothing to do with the added backslashes nor quotes. Those are all ASCII characters, and are added for different reasons. ensure_ascii only impacts non-ASCII characters, e.g. it causes ”漢字" to be output as "\u6f22\u5b57" instead of "漢字".

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.