Git Product home page Git Product logo

Comments (4)

puffyqi avatar puffyqi commented on July 18, 2024 1

@puffyqi has this answered your question?

Thanks for your help. I have tried and is able to work.

How I do:

  • Get all the pointers from a JSON Schema file:

    JsonNode schemanodes = JsonNode.Parse(schemadata);
    JsonPath jpath = JsonPath.Parse("$..[[email protected]=='date-time']");
    PathResult pathresults = jpath.Evaluate(schemanodes);
    NodeList nodeslist = pathresults.Matches;
    for (int i = 0; i < nodeslist.Count; i++)
    {
        string pointer = nodeslist[i].Location.AsJsonPointer();
        pointer_list.Add(pointer, new List<string>());
    }
  • Get the pointers (instanceLocation) from Evaluation Results with the help of above pointers:

    string schemalocationnode = node["schemaLocation"].ToString();
    string instancelocationnode = node["instanceLocation"].ToString();
    foreach (KeyValuePair<string, List<string>> entry in pointer_list)
    {
        string pointername = entry.Key;
    
        if (schemalocationnode.EndsWith(pointername))
        {
            List<string> datapointerslist = pointer_list[pointername];
            datapointerslist.Add(instancelocationnode);
        }
    }
  • Get the data values from datapointerslist (list of instancelocation) Using JPointer

    foreach (KeyValuePair<string, List<string>> entry in pointer_list)
    {
        List<string> pointers = entry.Value;
    
        if (pointers.Count > 0)
        {
            foreach (string pointerlocation in pointers)
            {
                JsonPointer pointer = JsonPointer.Parse(pointerlocation);
                JsonElement? element = pointer.Evaluate(jsondatadoc.RootElement);
                if (element == null)
                {
                    Console.WriteLine("cannot find pointer location: " + pointerlocation);
                }
                else
                {
                    Console.WriteLine("data value: " + element.ToString());
                    Console.WriteLine("pointer: " + pointerlocation);
                }
            }
        }
    }

In Summary,

  1. Get all the pointers in a JSON schema file where it have the format "date-time"
  2. When verify schema file with data file, use the EvaluationResults and loop the details array. In details array, look for those "schemaLocation" that contains the pointers and save its "instanceLocation" in a list
  3. Convert JSON data file to JSONDocument and loop the "instanceLocation" list to get the data values from each pointer in the list

from json-everything.

gregsdennis avatar gregsdennis commented on July 18, 2024

I would use JSON Path.

You'll need to have the schema in JsonNode form. Then use JsonPath.Net with the path $..[[email protected]=='date-time'].format.

Each result contains the node and the JSON Path to it. Select the paths and you can convert them to pointer strings with the .AsJsonPointer() extension.

from json-everything.

gregsdennis avatar gregsdennis commented on July 18, 2024

Oh! Also note that this won't catch format keywords in the schema root. The .. operation in JSON Path can't see the local node, only descendants.

from json-everything.

gregsdennis avatar gregsdennis commented on July 18, 2024

@puffyqi has this answered your question?

from json-everything.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.