Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

json pointers not resolved in python module

I have created a json file that has some values in a central area and then a sub area that uses pointers to get values from the central area. I am trying to load the json using the json module from python but when I load the file it doesn’t resolve the pointers to the desired values but rather acts as it they are just more text. How can I make it work to resolve pointers when they are there along with also working when there are no pointers?

Code:

import json
data = json.loads(open("test.json").read())
print(data)

#Output:
#{'ColoringLight': {'bg': '#ffffff', 'fg': '#4f4c4d', 'TokenText': '#544619', 'TokenKeyword': '#2f33ff', 'TokenPunctuation': '#dd8ced', 'TokenName': '#b6c23f', 'TokenOperator': '#f6a656', 'TokenComment': '#2cbe48', 'TokenLiteralNumber': '#ab4bbe', 'TokenLiteralString': '#f45159'}, 'syntax': {'Token.Text': {'$ref': '#/ColoringLight/TokenText'}, 'Token.Keyword': {'$ref': '#/ColoringLight/TokenKeyword'}, 'Token.Punctuation': {'$ref': '#/ColoringLight/TokenPunctuation'}, 'Token.Name.Attribute': {'$ref': '#/ColoringLight/TokenName'}, 'Token.Operator': {'$ref': '#/ColoringLight/TokenOperator'}, 'Token.Comment': {'$ref': '#/ColoringLight/TokenComment'}, 'Token.Literal.Number.Bin': {'$ref': '#/ColoringLight/TokenLiteralNumber'}, 'Token.Literal.String.Affix': {'$ref': '#/ColoringLight/TokenLiteralString'}}}

test.json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "ColoringLight":
    {
        "bg": "#ffffff",
        "fg": "#4f4c4d",
        "TokenText": "#544619",
        "TokenKeyword": "#2f33ff",
        "TokenPunctuation": "#dd8ced",
        "TokenName": "#b6c23f",
        "TokenOperator": "#f6a656",
        "TokenComment": "#2cbe48",
        "TokenLiteralNumber": "#ab4bbe",
        "TokenLiteralString": "#f45159"
    },

    

    "syntax":
    {
        "Token.Text": {"$ref": "#/ColoringLight/TokenText"},
        "Token.Keyword": {"$ref": "#/ColoringLight/TokenKeyword"},
        "Token.Punctuation": {"$ref": "#/ColoringLight/TokenPunctuation"},
        "Token.Name.Attribute": {"$ref": "#/ColoringLight/TokenName"},
        "Token.Operator": {"$ref": "#/ColoringLight/TokenOperator"},
        "Token.Comment": {"$ref": "#/ColoringLight/TokenComment"},
        "Token.Literal.Number.Bin": {"$ref": "#/ColoringLight/TokenLiteralNumber"},
        "Token.Literal.String.Affix": {"$ref": "#/ColoringLight/TokenLiteralString"}

    }
}

NOTE:

As pointed out after posting it turns out that json has no pointers built in

>Solution :

JSON references are not part of the JSON specification, but an extension. You could install a library like jsonref, then you could do:

import jsonref
data = jsonref.loads(open("test.json").read())
print(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading