How to extract all property paths in JSON file having specific value - json

How can I collect all property paths in a large JSON file containing a specific case-sensitive value?
Here's an example:
JSON:
{
"level1": {
"level1node1": "hit",
"level1node2": "miss",
"level1node3": {
"level2node1" : {
"level3node1": "hit",
"level3node2": {
"level4node1": "miss",
"level4node2": "hit"
}
}
}
}
}
Search text (case-sensitive): "hit"
Return C# string array:
[ "level1.level1node1",
"level1.level1node3.level2node1.level3node1",
"level1.level1node3.level2node1.level3node2.level4node2" ]
I'd like to use JSON.NET to get an array of matching node paths.

This does the trick:
void AddPath(List<string> propertyPaths, JToken jToken, string searchForValue) {
if (jToken is JValue && ((JValue)jToken).Value != null && ((JValue)jToken).Value.ToString() == searchForValue) {
propertyPaths.Add(jToken.Path);
} else if (jToken is JProperty) {
((JProperty)jToken).Values().ToList().ForEach(_ => AddMatch(propertyPaths, _, searchForValue));
} else if (jToken is JObject || jToken is JArray) {
if (jToken.HasValues) {
jToken.Children().ToList().ForEach(_ => AddMatch(propertyPaths, _, searchForValue));
}
}
}

Related

Newtonsoft.json SelectToken Replace differs from SelectTokens Replace in foreach with a NullReferenceException

Hope anybody could guide me here. I spend some hours on it and can't understand what's going on.
Mission: Replace a json element by a jsonpath search tag. (sort of $ref feature)
In my code example below i want to replace the value of DataReaderUser by a value found by the json path search $.UsersAndGroups.Users[?(#.Name == 'OMDASAccountUser')].Username . In this case it should result in the value "contoso\SVCSCOM-DO-OMDAS"
The code below works as expected.. the issue is below this code ..
https://dotnetfiddle.net/gEjggK
using System;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string json = #"{
""SQLServer"": {
""SQLReportingServices"": {
""AccountSettings"": {
""DataReaderUser"": {""$JsonPath"": ""$.UsersAndGroups.Users[?(#.Name == 'OMDASAccountUser')].Username""},
}
}
},
""UsersAndGroups"": {
""Users"": [
{
""Name"": ""OMActionAccountUser"",
""Username"": ""contoso\\SVCSCOM-DO-OMDAS"",
},
{
""Name"": ""OMDASAccountUser"",
""Username"": ""contoso\\SVCSCOM-DO-OMDAS"",
}
]
}
}";
JObject jo = JObject.Parse(json);
var JsonPath = jo.SelectToken("..$JsonPath");
JsonPath.Parent.Parent.Replace(jo.SelectToken(JsonPath.ToString()));
Console.WriteLine(jo.ToString());
}
}
The output will be :
{
"SQLServer": {
"SQLReportingServices": {
"AccountSettings": {
"DataReaderUser": "contoso\\SVCSCOM-DO-OMDAS"
}
}
},
"UsersAndGroups": {
"Users": [
{
"Name": "OMActionAccountUser",
"Username": "contoso\\SVCSCOM-DO-OMDAS"
},
{
"Name": "OMDASAccountUser",
"Username": "contoso\\SVCSCOM-DO-OMDAS"
}
]
}
}
Now the issue:
I want to do the same for all possible jsonpaths refers. So i use the SelectTokens and an foreach . But it looks like the behavior is different , the parents are null.
https://dotnetfiddle.net/lZW3XP
using System;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string json = #"{
""SQLServer"": {
""SQLReportingServices"": {
""AccountSettings"": {
""DataReaderUser"": {""$JsonPath"": ""$.UsersAndGroups.Users[?(#.Name == 'OMDASAccountUser')].Username""},
}
}
},
""UsersAndGroups"": {
""Users"": [
{
""Name"": ""OMActionAccountUser"",
""Username"": ""contoso\\SVCSCOM-DO-OMDAS"",
},
{
""Name"": ""OMDASAccountUser"",
""Username"": ""contoso\\SVCSCOM-DO-OMDAS"",
}
]
}
}";
JObject jo = JObject.Parse(json);
var JsonPaths = jo.SelectTokens("..$JsonPath");
foreach (var JsonPath in JsonPaths )
{
JsonPath.Parent.Parent.Replace(jo.SelectToken(JsonPath.ToString()));
}
Console.WriteLine(jo.ToString());
}
}
And the output:
Run-time exception (line 34): Object reference not set to an instance of an object.
Stack Trace:
[System.NullReferenceException: Object reference not set to an instance of an object.]
at Newtonsoft.Json.Linq.JsonPath.PathFilter.GetNextScanValue(JToken originalParent, JToken container, JToken value)
at Newtonsoft.Json.Linq.JsonPath.ScanFilter.<ExecuteFilter>d__4.MoveNext()
at Program.Main() :line 34
would be great to get some directions since i am spinning my head here.
michel
SelectTokens uses lazy evaluation and if you modify the token while enumerating all matches it can break in unexpected ways. A simple fix is to add ToArray() to force eager evaluation:
var JsonPaths = jo.SelectTokens("..$JsonPath").ToArray();

Extract parameters from nested Json

I have an json string, which looks like this:
{
\"request\": {
\"requestId\": \"dd92f43ec593d2d8db94193b7509f5cd\",
\"notificationType\": \"EntityAttribute\",
\"notificationSource\": \"ODS\"
},
\"entityattribute\": {
\"entityId\": \"123\",
\"attributeType\": \"DATE_OF_BIRTH\"
}
}
I want to deserialized entityattribute to an object:
public class EntityAttributeNotification {
private String attributeType;
private String entityId;
}
One way is to extract entityId and attributeType first using the json path(i.e entityattribute/entityId)and create an object EntityAttributeNotification.
I want to know if there is a way to directly deserialized entityattribute to EntityAttributeNotification.
I have also tried with JsonMixin annotation but this does not apply here.
Through the following method you can extract Parameters and Values of nested JSON .
const object1 ={
"request": {
"requestId": "dd92f43ec593d2d8db94193b7509f5cd",
"notificationType": "EntityAttribute",
"notificationSource": "ODS"
},
"entityattribute": {
"entityId": "123",
"attributeType": "DATE_OF_BIRTH"
}
};
var keys = [];
for (let [key, value] of Object.entries(object1)) {
if(typeof value == 'object'){
keys.push(key);
for (let [key1, value1] of Object.entries(value)) {
keys.push(key1);
}
}
else{
keys.push(key);
}
}
console.log(keys);

How to map json entities?

There is a way to map json objects in react-native? For example, let's say I receive the following object:
{
"dados": {
"ultimaAtualizacao": {
"nome": "Lala"
}
"nascimento": "01/01/2001"
}
}
I want to map it like this:
{
"data": {
"lastUpdate": {
"name": "Lala"
}
"dob": "01/01/2001"
}
}
Considering that fetch returns the first object, this would be possible doing the following code:
myService.get(url).then(response => this.mapObject(response, []));
//Considering state is initialized and I have the mapedEntity to return correct properties
mapObject(jsonObject, parentProperty) {
for (let property in jsonObject) {
if (Array.isArray(jsonObject[property])) {
this.mapArray(jsonObject[property], [...parentProperty,property]); //Would be something like mapObject function
} else if (typeof jsonObject[property] === 'object') {
this.mapObject(jsonObject[property],[...parentProperty,property]);
} else {
let prop = this.state;
for(let k of parentProperty) {
prop = prop[mapedEntity[k]];
}
prop[entidadeMapeada[property]] = jsonObject[property];
}
}
}
There is someway simplier to achieve this?

Recursively removing whitespace from JSON field names in Groovy

I have a Groovy process that is receiving troublesome JSON that has attribute/field names containing whitespaces:
{
"leg bone" : false,
"connected to the" : {
"arm bones " : [
{
" fizz" : "buzz",
"well hello" : "there"
}
]
}
}
Above, fields such as "leg bone" and "well hello" are causing issues during processing (even though they are technically legal JSON fields). So I want to scan each field (recursively, or in nested fashion) in the incoming JSON and string replace any whitespace with an underscore ("_"). Hence, the above JSON would be converted into:
{
"leg_bone" : false,
"connected_to__the" : {
"arm_bones_" : [
{
"_fizz" : "buzz",
"well_hello" : "there"
}
]
}
}
Typically I use a JsonSlurper for parsing JSON strings into maps, but I can't seem to figure out how to get the recursion correct. Here's my best attempt so far:
// In reality 'incomingJson' isn't hardcoded as a string literal, but this helps make my actual use case
// an SSCCE.
class JsonMapExperiments {
static void main(String[] args) {
String incomingJson = """
{
"leg bone" : false,
"connected to the" : {
"arm bones " : [
{
" fizz" : "buzz",
"well hello" : "there"
}
]
}
}
"""
String fixedJson = fixWhitespaces(new JsonSlurper().parseText(incomingJson))
println fixedJson
}
static String fixWhitespaces(def jsonMap) {
def fixedMap = [:]
String regex = ""
jsonMap.each { key, value ->
String fixedKey = key.replaceAll('\\s+', '_')
String fixedValue
if(value in Map) {
fixedValue = fixWhitespaces(value)
} else {
fixedValue = value
}
fixedMap[fixedKey] = fixedValue
}
new JsonBuilder(fixedMap).toString()
}
}
When this runs, the final output is:
{"connected_to_the":"{\"arm_bones_\":\"[{ fizz=buzz, well hello=there}]\"}","leg_bone":"false"}
Which is kinda/sorta close, but not exactly what I need. Any ideas?
Given your input and this script:
def fixWhitespacesInTree(def tree) {
switch (tree) {
case Map:
return tree.collectEntries { k, v ->
[(k.replaceAll('\\s+', '_')):fixWhitespacesInTree(v)]
}
case Collection:
return tree.collect { e -> fixWhitespacesInTree(e) }
default :
return tree
}
}
def fixWhitespacesInJson(def jsonString) {
def tree = new JsonSlurper().parseText(jsonString)
def fixedTree = fixWhitespacesInTree(tree)
new JsonBuilder(fixedTree).toString()
}
println fixWhitespacesInJson(json)
I got the following results:
{"connected_to_the":{"arm_bones_":[{"_fizz":"buzz","well_hello":"there"}]},"leg_bone":false}
I would, however, suggest that you change the regular expression \\s+ to just \\s. In the former case. if you have two JSON properties at the same level, one called " fizz" and the other called " fizz" then the translated keys will both be "_fizz" and one will overwrite the other in the final result. In the latter case, the translated keys will be "_fizz" and "__fizz" respectively, and the original content will be preserved.

Adding property to JSON object in Javascript

I am using xml2js to convert xml to js object and add new nodes to the content
Ex1:
<abc>
<my-node>123</my-node>
<my-node>456</my-node>
</abc>
Ex2:
<abc>
<my-node>123</my-node>
</abc>
In the Ex1, the my-node property will be an array whereas in the Ex2, it will be non-array item.
How to add extra my-node to the same. I can do in below format but looking for better solution?
if(typeof abc.my-node == Array){
abc.my-node.push(xxx);
} else {
//create empty array
//add existing element
//add xxx
//set the array to json object
}
If you use
function addProp(obj, propName, value) {
if (propName in obj) {
if (obj[propName] instanceof Array) {
obj[propName].push(value);
}
else if (typeof obj[propName] !== 'object') {
obj[propName] = [obj[propName], value];
}
}
else {
obj[propName] = value;
}
}
var abc = {};
console.log(JSON.stringify(abc));
addProp(abc, 'my-node', 123);
console.log(JSON.stringify(abc));
addProp(abc, 'my-node', 456);
console.log(JSON.stringify(abc));
addProp(abc, 'my-node', 789);
console.log(JSON.stringify(abc));
then the result is
{}
{"my-node":123}
{"my-node":[123,456]}
{"my-node":[123,456,789]}