Reputation: 177
I need to filter JSON that comes from a server. The problem is that the property name is mutable so I can't set it beforehand. Is there a clean way of doing that?
Here is an example JSON that my app is getting:
{0: {SchemaVersion: "15.0.0.0", LibraryVersion: "16.0.8908.1207", ErrorInfo: null, TraceCorrelationId: "e2c3de9e-20a8-0000-a44c-e76e7f8e0cea"}
1: 1
2: {IsNull: false}
3: 5
4: {IsNull: false}
5: 14
6: {IsNull: false}
7: 18
8: {IsNull: false}
9: 31
10: {IsNull: false}
11: 52
12: {IsNull: false}
13: 57
14:
8899eccf-b0b6-4a9e-978c-cd2f14f4ef2d849ad13c-ea4f-4cb7-b227-5b66ef558040:
{ElapsedTime: 181
Properties: {RowLimit: 3, SourceId: "/Guid(4b9d73a0-993b-4428-989a-6b48dad9687e)/", CorrelationId: "/Guid(e2c3de9e-20a8-0000-a44c-e76e7f8e0cea)/", WasGroupRestricted: false, IsPartial: false, …}
QueryErrors: null
QueryId: "75d8ce01-ae02-41b9-9393-0038ee4a3ba5"
ResultTables: [{…}]
SpellingSuggestion: ""
TriggeredRules: []
_ObjectType_: "Microsoft.SharePoint.Client.Search.Query.ResultTableCollection"
}
15: 53
16: {HasException: false, ErrorInfo: null}
}
I need to extract an object with id 14 by checking its "ObjectType" property. but the property name (in the example: "8899eccf-b0b6-4a9e-978c-cd2f14f4ef2d849ad13c-ea4f-4cb7-b227-5b66ef558040" is different every time.
Upvotes: 0
Views: 91
Reputation: 136
Using Object.keys() returns an array of all the keys of your object. With the keys you can then filter by properties you're looking for.
let keys = Object.keys(myJson)
let foundObj = keys.filter(function(k) {
return k['_ObjectType_'] === 'Microsoft.SharePoint.Client.Search.Query.ResultTableCollection'
})
Upvotes: 1