Home > Mobile >  json pointers not resolved in python module
json pointers not resolved in python module

Time:07-10

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:

{
    "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

CodePudding user response:

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)
  • Related