# jsonSchema to validate object with random IDs for properties

**URL:** <https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167>\
**Category:** Help Hub\
**Tags:** tests, jsonschema\
**Created:** [April 27, 2021, 7:41pm UTC](https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167 "2021-04-27T19:41:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bezzea](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bezzea](https://community.postman.com/u/bezzea)\
**Post date:** [April 27, 2021, 7:41pm UTC](https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167/1 "2021-04-27T19:41:50Z")

</div>

Hey everyone, I am trying to figure out how can I use the jsonSchema ([Schema validation — Postman Quick Reference Guide Version 1.6.10 - February 2021 documentation](https://postman-quick-reference-guide.readthedocs.io/en/latest/schema-validation.html)), to validate a response body that consists of the following example:

```
{
    "Role": {
        "16": {
            "id": 16,
        },

        "108": {
            "id": 108,

        },
}

```

We have a nested object and trying to follow up the documentation I linked above, I get stuck at those random IDs for each object. How can I write something for those properties?

```
const expectedData = 
{
    "type": "object",
    "properties":{
        "Role": {
            "type":"object",
            "properties":{
                "16": {
                    "type":"object",
                    "properties":{
                        "id": {
                            "type": "number", "null"
                    }   
                }
        }
    }
}

```

Ignore the details, the piece above is not ready, just the structure is what I am interested in.

---

<div class="post-metadata">

**Author:** ![meenakshi.dhanani](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meenakshi.dhanani/32/5980_2.png) [@meenakshi.dhanani](https://community.postman.com/u/meenakshi.dhanani)\
**Post date:** [April 28, 2021, 12:53pm UTC](https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167/2 "2021-04-28T12:53:42Z")

</div>

Hi @bezzea,  
Welcome 😃  
I found this - [jsonschema - JSON Schema for object with keys of random numbers - Stack Overflow](https://stackoverflow.com/questions/54574530/json-schema-for-object-with-keys-of-random-numbers) and this - [object — Understanding JSON Schema 7.0 documentation](https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties)

With pattern properties, the key is matched to a regex and the schema can be validated for that regex key. Hope that can solve the issue.

Thanks for posting, I learned something new today 🤓

---

<div class="post-metadata">

**Author:** ![bezzea](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bezzea](https://community.postman.com/u/bezzea)\
**Post date:** [April 28, 2021, 4:50pm UTC](https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167/3 "2021-04-28T16:50:09Z")

</div>

Hello and thanks for your answer,

Not really sure if I understood it correctly, this is what I came up with and it still throws an error:

> There was an error in evaluating the test script: SyntaxError: Unexpected token ‘}’

```
const response = pm.response.json()
const expectedData = 
{
    "type": "object",
    "properties": {
        "Role": {"type": "object"},
            "patternProperties": {
                "^[0-9]+$": {"type": "integer"},
                    "id": {"type":"number"},
            }
    }
}

pm.test("Received", function(){
    pm.response.to.have.jsonSchema(expectedData)

})

```

Could this be because my IDs are indeed strings, but I expect postman to read those ID’s as objects?

I have also noticed that I told postman to expect for each properties a series of numbers,strings between the object braces and that I believe is wrong.

EDIT:  
Solved the problem by adding another type for properties between {} like so `"originDate": {"type":["string","null"]},`  
It had to see the second type instead of just adding the types one after another with comma symbol.

---

<div class="post-metadata">

**Author:** ![hannah.neil](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/hannah.neil/32/4477_2.png) [@hannah.neil](https://community.postman.com/u/hannah.neil)\
**Post date:** [April 28, 2021, 8:47pm UTC](https://community.postman.com/t/jsonschema-to-validate-object-with-random-ids-for-properties/23167/4 "2021-04-28T20:47:29Z")

</div>

Glad you found the solution! I marked it so that other Postman users can find this. I’m also moving the thread over to help.

Welcome to the community, @bezzea !
