JSON - filter array by string - json

I am working in MS Power Automate (Flow) and I want to filter a JSON array by a string to retrieve the value...
here is a subset of my JSON Data (this data has been extracted from a SharePoint list by a Flow Get Items step):
[
{
"#odata.etag": "\"2\"",
"ItemInternalId": "1",
"ID": 1,
"Setting1": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 0,
"Value": "Email Addresses"
},
"Setting1#Id": 0,
"Setting2": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 0,
"Value": "Retail Marketing Team Email"
},
"Setting2#Id": 0,
"Value1": "person1#company.co.uk; person2#company.co.uk; "
},
{
"#odata.etag": "\"2\"",
"ItemInternalId": "2",
"ID": 2,
"Setting1": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 0,
"Value": "Email Addresses"
},
"Setting1#Id": 0,
"Setting2": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 1,
"Value": "Social Media Email"
},
"Setting2#Id": 1,
"Value1": "paidsocial#company.co.uk; person3#company.co.uk; person4#company.co.uk;"
},
....
]
I need to filter that on the field Setting2 = "search string", and return Value1 by matching a string
So, for Setting2 I would use 'Retail Marketing Team Email' and extract the value of the Value1 field which is '*person1#company.co.uk; person2#company.co.uk; *'
Now, with $..['Value1'] I can get all of the Value1 values, and with filtering I should be able to retrieve a specific Value1 element...
I have tried this:
$.[?(#.Setting2 == 'Retail Marketing Team Email')]['Value1']
and
$..['Setting2'].['Value' == 'Retail Marketing Team Email']?['Value']
and several other variations but never seem to get any data back, or OI get a JSON Parse error
what is the right way to go about this?

Related

Jsonpath - Accessing array item using expression

I am using AWS Step Functions which utilizes JSONPath for providing JSON paths. I have the following input :
{
"response": {
"isSuccess": true,
"error": "",
"body": {
"count": 2,
"fields": [
{
"fieldId": 1,
"tabId": 100,
"title": "First Name"
},
{
"fieldId": 2,
"tabId": 100,
"title": "Last Name"
}
]
}
},
"iteration": {
"totalCount": 2,
"currentCount": 0,
"step": 1
}
}
I want to query the fields array as:
$.response.body.fields[$.iteration.currentCount]
The value of currentCount is incremented by 1 as part of an iteration.
I am getting an invalid XPath exception when trying to use the above.
Can someone please advice on how to provide a dynamic property value to read array values?
As described on https://github.com/json-path/JsonPath#operators you can index an array with a number only. However, you can use a filter expression to select a specific item from the array as follows.
Assuming you have another field that denotes the index such as:
{
"index": 0,
"fieldId": 2,
"tabId": 100,
"title": "Last Name"
}
You can then do
$.response.body.fields[?(#.index==$.iteration.currentCount)]

Render Nested json objects in React Native

In my render() method i need to parse nested json objects. See the portion of render and json structure below.
I access Last Name with {params.name_last}.
How will i access items under team, like team.name_first
render() {
let { params } = this.props.navigation.state
<Text>{params.name_last}</Text>
}
[
{
"id": 1,
"name_first": "Name first 1",
"name_middle": "",
"name_last": "Name last 1",
"name_suffix": "",
"phone": "888-8888",
"fax": "888-8888",
"updated_at": "2015-11-02T21:42:42.000Z",
"team": [
{
"id": 16,
"name_first": "aaa",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
},
{
"id": 28,
"name_first": "aaa",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
},
{
"id": 29,
"name_first": "aaa ",
"name_middle": "",
"name_last": "bbb",
"name_suffix": ""
}
]
}
]
Since team is an array, you need to either access a specific entry in the array, or iterate over the entire thing.
To reach a specific property in the nested array entry (assuming you want the object at index i):
params.team[i].name_first
To create an array of first names:
params.team.map(x => x.name_first)

Rails 4: Trying to use Ancestry with jstree

I am new to Rails and trying to populate a jstree in the browser with JSON from the Ancestry gem (see https://github.com/stefankroes/ancestry). JSON is the go-between. Where I am stuck is translating the JSON produced (from my Tree model) by Ancestry:
<%= #trees.arrange_serializable.to_json %>
That gives me nice JSON as follows (fragment only, apologies for the nonsense values):
[
{
"id": 2,
"name": "Milk",
"note": "No details available",
"created_at": "2014-09-20T13:22:03.262Z",
"updated_at": "2014-09-20T13:48:46.301Z",
"value": "3.06",
"ancestry": null,
"children": [
{
"id": 1,
"name": "Farms",
"note": "Some note here",
"created_at": "2014-09-20T13:05:22.186Z",
"updated_at": "2014-10-03T11:30:39.029Z",
"value": "432.0",
"ancestry": "2",
"children": [
{
"id": 8,
"name": "Zog",
"note": "Orc",
"created_at": "2014-09-27T22:11:20.874Z",
"updated_at": "2014-10-03T11:30:38.989Z",
"value": "11.0",
"ancestry": "2/1",
"children": [
{
"id": 14,
"name": "Planes",
"note": "",
"created_at": "2014-10-04T01:01:12.890Z",
"updated_at": "2014-10-04T04:51:20.873Z",
"value": "422.0",
"ancestry": "2/1/8",
"children": []
}
]
},
but the jstree plugin requires a particular format for the JSON as follows (from http://www.jstree.com/docs/json/):
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
While the source hierarchical structure is perfect, I need to replace the attribute "name" in the original with "text" in the output, plus a few other changes.
At the back of my mind I feel there should be something elegant (like XSLT for XML) to translate JSON#1 into JSON#2 but I can't find any examples that do this.

Json Slupper assert and extract

I'm using the next Json
{
"ID": 8,
"MenuItems": [
{
"ID": 38,
"Name": "Home",
"URL": "{\"PageLayout\":\"Home\",\"Icon\":\"home\"}",
"Culture": "en",
"Children": null
},
{
"ID": 534,
"Name": "GUIDE ",
"URL": "{''PageLayout'':''Page A'', ''Icon'':''A''}",
"MenuType": 1,
"PageID": 0,
"Culture": "en",
"Children": [
{
"ID": 6,
"Name": "Form A",
"URL": "[''Type'':''Form A'',''Icon'':''Form'',''ItemID'':\"358\"]",
"Culture": "he",
"RuleID": 0
},
{
"ID": 60,
"Name": "Drama",
"URL": "[''Type'':''Form B'',''Icon'':''Form'',''ItemID'':\"3759\"]",
"Culture": "en",
"RuleID": 0
}
]
}
]
}
i'm using Groovy script in soapUI and i need to:
Assert the exitance of node that has the name GUIDE
Extract a list of all Itemsid
You can parse the JSON content using JsonSlurper and then work with the results like so:
import groovy.json.JsonSlurper
// Assuming your JSON is stored in "jsonString"
def jsonContent = new JsonSlurper().parseText(jsonString)
// Assert node exists with name GUIDE
assert(jsonContent.MenuItems.Name.contains("GUIDE"))
// Get list of ItemIDs
def itemList = jsonContent.MenuItems.Children.URL.ItemID[0].toList()
// List the items
itemList.each {log.info it}
Note that the above will fail given your current example, because of a few issues. Firstly, Name contains "GUIDE " (trailing space) rather than "GUIDE" (so you will need to adapt accordingly). Secondly, it is invalid JSON; the URL nodes have various erroneous characters.
On a side note, if you first need to retrieve your JSON from the response associated with a previous TestStep (say one called "SendMessage") within the existing testCase, this can be done as follows:
def jsonString = context.testCase.getTestStepByName("SendMessage").testRequest.response.getContentAsString()

Backbone.js Collections

I am working with backbone.js. I am trying to send a request to restful service i am getting the resultset as json object as shown
{
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}.
I am putting the json object extracting and setting it to collection but I am not able to get particular model from the collection with specific id.
If you want to make your model can be identified with id. You have to set the id in the attributes hash:
{
"id": 1001,
"Msgs": [
"Alert",
"Not"
],
"MessageStatus": [
"Active",
"Inactive"
],
"date": {
"From": "2013-04-25",
"To": "2013-06-25"
},
"Mlist": {
"Status": "PND",
"Role": "Admin,User",
"To": "2013-06-24",
"Id": 6,
"Datecreated": "2013-06-24",
"Title": "Title5",
"From": "2013-06-20"
}
}
Then using backbone collection "findWhere" method to get the specific id model.
//assume msgCollection is which you put the models
var model = msgCollection.findWhere({id: 1001});
Hope this is helpful for you.
I think you need to set your model and parse your input data. Checkout http://backbonejs.org/#Collection-model, http://backbonejs.org/#Model-parse and http://backbonejs.org/#Collection-parse.