python json.loads / json.load truncates nested json objects?

2024/9/21 3:15:30

given the following code:

import json 
foo = '{"root":"cfb-score","children":{"gamecode":{"attribute":"global-id"},"gamestate":{"attribute":"status-id","attribute":"status","attribute":"quarter","attribute":"minutes","attribute":"seconds","attribute":"team-possession-id","attribute":"yards-from-goal","attribute":"down","attribute":"distance","attribute":"segment-number","attribute":"active-state"},"gametype":{"attribute":"type","attribute":"detail"},"stadium":{"attribute":"name","attribute":"city","attribute":"state"},"visiting-team:team-name":{"attribute":"alias"},"visiting-team:team-code":{"attribute":"global-id"},"visiting-team:team-rank":{"attribute":"rank"}}}'bar = json.loads(foo)
print json.dumps(bar)

all the lowest level 'children' are truncated (or maybe more likely overwritten) except the last when using json.loads or json.load. Why? the json is well formed and can be validated here: http://json.parser.online.fr/

a chunk of the input:

"children" : {"gamecode" : {"attribute" :  "global-id"},"gamestate" : {"attribute" : "status-id", "attribute" : "status", "attribute" : "quarter", "attribute" : "minutes", "attribute" : "seconds", "attribute" : "team-possession-id", "attribute" : "yards-from-goal", "attribute" : "down", "attribute" : "distance", "attribute" : "segment-number", "attribute" : "active-state" }, 

turns to this chunk of output:

"children" : {"gamecode" : {"attribute" :  "global-id"},"gamestate" : {"attribute" : "active-state" }, 
Answer

The JSON is well-formed (i.e., syntactically valid) but semantically invalid. You can't have multiple keys with the same value in a Python dict, nor in a JS object. If you validate that input at the page you linked to, you'll see that the "JS eval" pane also shows the "truncated" data.

If you want multiple values, change the format of your data to have one key with an array value:

"gamestate" : {"attributes": ["status-id", "status", "quarter", ...]}, 

(Or, depending on what the overall data is like, you could just have the gamestate key directly link to an array instead of having another layer of nesting under the attribute key.)

https://en.xdnf.cn/q/119243.html

Related Q&A

How to make an encrypted executable file

I have made a tool/program on Ubuntu written in Python. I want to give this to my friend to test on his PC, but I dont want to share the source code.This program has many folders and many .py files. Is…

Organizing pythonic dictionaries for a JSON schema validation

Scenario: I am trying to create a JSON schema validator in python. In this case, I am building a dictionary which contain the information that will be used for the validation.Code:import json import os…

Scraping a specific website with a search box and javascripts in Python

On the website https://sray.arabesque.com/dashboard there is a search box "input" in html. I want to enter a company name in the search box, choose the first suggestion for that name in the d…

Uppercasing letters after ., ! and ? signs in Python

I have been searching Stack Overflow but cannot find the proper code for correcting e.g."hello! are you tired? no, not at all!"Into:"Hello! Are you tired? No, not at all!"

Why does list() function is not letting me change the list [duplicate]

This question already has answers here:How do I clone a list so that it doesnt change unexpectedly after assignment?(24 answers)Python pass by value with nested lists?(1 answer)Closed 2 years ago.If …

How can I explicitly see what self does in python?

Ive read somewhere that the use of ‘self’ in Python converts myobject.method (arg1, arg2) into MyClass.method(myobject, arg1, arg2). Does anyone know how I can prove this? Is it only possible if I…

Recieve global variable (Cython)

I am using Cython in jupyter notebook. As I know, Cython compiles def functions.But when I want to call function with global variable it doesnt see it. Are there any method to call function with variab…

Counting elements in specified column of a .csv file

I am programming in Python I want to count how many times each word appears in a column. Coulmn 4 of my .csv file contains cca. 7 different words and need to know how many times each one appears. Eg. t…

Why does genexp(generator expression) is called genexp? not iterexp?

A generator is a special kind of iterator, and it has some methods that an normal iterator doesnt have such as send(), close()... etc. One can get a generator by using a genexp like below:g=(i for i in…

why does no picture show

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg if __name__ == "__main__":fig1 = ...print("start plotting")canvas = FigureCanvasQTAgg(fig1)canvas.draw()canvas.show(…