Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print_mongo_paths #44

Open
swuecho opened this issue Jan 31, 2024 · 1 comment
Open

print_mongo_paths #44

swuecho opened this issue Jan 31, 2024 · 1 comment

Comments

@swuecho
Copy link
Owner

swuecho commented Jan 31, 2024

def print_mongo_paths(json_object, current_path=""):
    if isinstance(json_object, dict):
        for key, value in json_object.items():
            new_path = f"{current_path}.{key}" if current_path else key
            print_mongo_paths(value, new_path)
    elif isinstance(json_object, list):
        for i, item in enumerate(json_object):
            new_path = f"{current_path}.{i}" if current_path else str(i)
            print_mongo_paths(item, new_path)
    else:
        print(current_path)

# Example JSON object
example_json = {
    "file": [
        {
            "extraction": {
                "signature": {
                    "extraction": [
                        {
                            "signature": {
                                "extraction": {
                                    "normalized_value": "example_value"
                                }
                            }
                        }
                    ]
                }
            }
        }
    ]
}

# Print paths in MongoDB query format
print_mongo_paths(example_json)
@swuecho
Copy link
Owner Author

swuecho commented Jan 31, 2024

import play.api.libs.json.{JsObject, JsArray, JsValue}

object JsonPathPrinter {

  def printMongoPaths(json: JsValue, currentPath: String = ""): Unit = {
    json match {
      case obj: JsObject =>
        obj.fields.foreach { case (key, value) =>
          val newPath = if (currentPath.isEmpty) key else s"$currentPath.$key"
          printMongoPaths(value, newPath)
        }
      case arr: JsArray =>
        arr.value.zipWithIndex.foreach { case (item, index) =>
          val newPath = if (currentPath.isEmpty) index.toString else s"$currentPath.$index"
          printMongoPaths(item, newPath)
        }
      case _ =>
        println(currentPath)
    }
  }

  def main(args: Array[String]): Unit = {
    // Example JSON object
    val exampleJson: JsValue = play.api.libs.json.Json.parse("""
      |{
      |  "file": [
      |    {
      |      "extraction": {
      |        "signature": {
      |          "extraction": [
      |            {
      |              "signature": {
      |                "extraction": {
      |                  "normalized_value": "example_value"
      |                }
      |              }
      |            }
      |          ]
      |        }
      |      }
      |    }
      |  ]
      |}
      """.stripMargin)

    // Print paths in MongoDB query format
    printMongoPaths(exampleJson)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant