0

I have a JSON data of like this. I need to filter only name and uuid from this data. I have used JSON filters data["main_cat"]["data"]["name"] but I didn't get value to the key name here is the json data.

[
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/203/properties",
            "create_relationship": "http://localhost:7474/db/data/node/203/relationships",
            "labels": "http://localhost:7474/db/data/node/203/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/203/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/203/relationships/out",
            "property": "http://localhost:7474/db/data/node/203/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/203/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/203/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/203/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/203/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/203/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 203,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/203/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/203",
            "data": {
                "timestamp": 1439216657489,
                "name": "grocery & staples",
                "uuid": "7be98425-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    },
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/207/properties",
            "create_relationship": "http://localhost:7474/db/data/node/207/relationships",
            "labels": "http://localhost:7474/db/data/node/207/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/207/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/207/relationships/out",
            "property": "http://localhost:7474/db/data/node/207/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/207/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/207/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/207/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/207/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/207/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 207,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/207/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/207",
            "data": {
                "timestamp": 1439216657489,
                "name": "health care",
                "uuid": "7be98421-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    },
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/208/properties",
            "create_relationship": "http://localhost:7474/db/data/node/208/relationships",
            "labels": "http://localhost:7474/db/data/node/208/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/208/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/208/relationships/out",
            "property": "http://localhost:7474/db/data/node/208/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/208/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/208/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/208/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/208/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/208/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 208,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/208/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/208",
            "data": {
                "timestamp": 1439216657489,
                "name": "household",
                "uuid": "7be9d243-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    },
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/190/properties",
            "create_relationship": "http://localhost:7474/db/data/node/190/relationships",
            "labels": "http://localhost:7474/db/data/node/190/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/190/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/190/relationships/out",
            "property": "http://localhost:7474/db/data/node/190/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/190/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/190/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/190/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/190/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/190/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 190,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/190/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/190",
            "data": {
                "timestamp": 1439216657489,
                "name": "food mart",
                "uuid": "7be90ef4-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    },
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/171/properties",
            "create_relationship": "http://localhost:7474/db/data/node/171/relationships",
            "labels": "http://localhost:7474/db/data/node/171/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/171/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/171/relationships/out",
            "property": "http://localhost:7474/db/data/node/171/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/171/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/171/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/171/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/171/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/171/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 171,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/171/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/171",
            "data": {
                "timestamp": 1439216657489,
                "name": "beauty care & personal care",
                "uuid": "7be872b3-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    },
    {
        "main_cat": {
            "properties": "http://localhost:7474/db/data/node/179/properties",
            "create_relationship": "http://localhost:7474/db/data/node/179/relationships",
            "labels": "http://localhost:7474/db/data/node/179/labels",
            "incoming_relationships": "http://localhost:7474/db/data/node/179/relationships/in",
            "outgoing_relationships": "http://localhost:7474/db/data/node/179/relationships/out",
            "property": "http://localhost:7474/db/data/node/179/properties/{key}",
            "paged_traverse": "http://localhost:7474/db/data/node/179/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/179/relationships/in/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/179/traverse/{returnType}",
            "all_relationships": "http://localhost:7474/db/data/node/179/relationships/all",
            "extensions": {},
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/179/relationships/out/{-list|&|types}",
            "metadata": {
                "id": 179,
                "labels": [
                    "Category"
                ]
            },
            "all_typed_relationships": "http://localhost:7474/db/data/node/179/relationships/all/{-list|&|types}",
            "self": "http://localhost:7474/db/data/node/179",
            "data": {
                "timestamp": 1439216657489,
                "name": "fruits and vegetables",
                "uuid": "7be93603-3f6b-11e5-83a2-ec0ec46eeadd",
                "main": true
            }
        }
    }
]

3 Answers 3

2

Assuming you have the JSON object parsed into json_map, another way to solve this would be:

(map #(select-keys (get-in % [:main_cat :data]) [:uuid :name]) json_map)

This is first going into your map via get-in and just grabbing at that one sub-hash you need. Then it's using select-keys to grab the keys it needs.

This won't be as straightforward when you need data outside of that map. In which case I suggest using something like Balagan for hash querying.

Sign up to request clarification or add additional context in comments.

Comments

0

This is how I would select name and uuid from a sequence of maps.

(let [data [{"main_cat" ;; keys elided for brevity
             {"self" "http//localhost7474/db/data/node/207",
              "data" {"timestamp" 1439216657489,
                      "name"      "health care",
                      "uuid"      "7be98421-3f6b-11e5-83a2-ec0ec46eeadd",
                      "main"      true}}}]]
  (map (fn [m] (-> m (get-in ["main_cat" "data"]) (select-keys ["name" "uuid"]))) data))
;=> ({"name" "health care", "uuid" "7be98421-3f6b-11e5-83a2-ec0ec46eeadd"})

Comments

0

Please note that I am also not an expert in Clojure, but here is how I would solve it (using data.json), wrapped in a function:

(defn filter-json [json-string]
 (let [data (json/read-str json-string :key-fn keyword)]
  (map #(dissoc (:data (:main_cat %)) :timestamp :main) data)))

The result of your posted data would be:

({:name "grocery & staples", :uuid "7be98425-3f6b-11e5-83a2-ec0ec46eeadd"} {:name "health care", :uuid "7be98421-3f6b-11e5-83a2-ec0ec46eeadd"} {:name "household", :uuid "7be9d243-3f6b-11e5-83a2-ec0ec46eeadd"} {:name "food mart", :uuid "7be90ef4-3f6b-11e5-83a2-ec0ec46eeadd"} {:name "beauty care & personal care", :uuid "7be872b3-3f6b-11e5-83a2-ec0ec46eeadd"} {:name "fruits and vegetables", :uuid "7be93603-3f6b-11e5-83a2-ec0ec46eeadd"})

You first read the JSON String with read-str (if you not already did) and specify, that you would like to access the structure with keywords. Then you can use the keywords to narrow it down until you get to the "data" section, where you dissoc the unwanted properties.

I think this solution can be optimized, I am looking forward to every solution for an improvement!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.