As input I have list of all objects where each object has properties:
name
children(object.children is the list of all children)
parent(null if object is top level)
How correctly to use Groovy JSONBuilder to render such data(depth of tree is unlimited):
-Object 1
--Object 1.1
---Object 1.1.1
--Object 1.2
-Object 2
in JSON format it should be something like this:
[
{
"name":"Object1",
"children":[
{
"name":"Object1.1",
"children":[
{
"name":"Object 1.1.1",
"children":[]
}
]
},
{
"name":"Object1.2",
"children":[]
}
]
},
{
"name":"Object2","children":[]
}
]
This is needed to build JSON for extJS component which will display this tree. Thanks for your help!
Store this structure in a object say jsonContainer and try following piece of code
render jsonContainer as grails.converters.deep.JSON
Hope this helps.
Add this property to your Config.groovy file:
grails.converters.json.default.deep=true
The Grails Doc
is a little out of date but the relevant property is listed there under 'Configuration Options'
Related
I am trying to fetch data from Hbase using GetHBase process and the result is as below
{
"row" : "r5",
"cells" : {
"cf1:ABC:" : "V1",
"cf1:DEF" : "V2",
"cf1:HIJ" : "V3",
"cf1:KLM" : "V4"
}
}
Now, I want to filter the json for specific columns
expected result
{"DEF":"V2","KLM":"V4"}
Flow tried
GetHbase --> evaluateJsonPath
Just use JoltTransformJSON, set Jolt Transformation DSL as Shift and set Jolt Specification as:
{
"cells": {
"cf1:DEF": "DEF",
"cf1:KLM": "KLM"
}
}
this will transform your input json into your desired json.
Jolt Transform is an open source library that helps you transform Json documents using other Json documents as specification.
If you'd like to parameterize them, you can do that using parameters using NiFi 1.10.0 like so:
{
"cells": {
"cf1:#{param1}": "#{param1}",
"cf1:#{param2}": "#{param2}"
}
}
Or using NiFi 1.4.0+ with expression language and the variable registry like so:
{
"cells": {
"cf1:${param1}": "${param1}",
"cf1:${param2}": "${param2}"
}
}
Everything is linked to a documentation so you can read about it all :)
I have some XML that I want to transform into a Map. I used the middle way of transforming XML to JSON and then greating a map of the json:
import org.json.XML
import groovy.json.JsonSlurper
Map parseXml(String input) {
String json = XML.toJSONObject(input).toString()
new JsonSlurper().parseText(json)
}
But when you have name spacing, it does not get removed.
eg.
<ns2:someObject xmlns:ns2="http://someLink">
<someOtherObject>
<something>SOME_THING</something>
</someOtherObject>
<someOtherObject>
<something>SOME_THING_ELSE</something>
</someOtherObject>
</ns2:someObject>
will end up in
{
"ns2:someObject": {
"xmlns:ns2": "http://someLink",
"someOtherObject": [
{
"something": "SOME_THING"
},
{
"something": "SOME_THING_ELSE"
}
]
}
}
But I want it to end up like this:
{
"someObject": [
{
"something": "SOME_THING"
},
{
"something": "SOME_THING_ELSE"
}
]
}
Does anyone have an idea how I can achive that without reinventing the wheel?
I already found a post a bit similar to mine, but it has a different approach, that's why I asked my question.
The example I gave is just an example, but there dan be multiple entries of someObjects which the given answer in the other post does not include.
Secondly it does iterate over the XML and creates a map of it - that is what I meant with reinventing the wheel. I am sure there must be a library doing exactly that already, so for me it seems wrong to write the parsing code myself.
Many thanks
In a Jmeter Script, I need to process a http response e manipulate a json for send in next request, because actually this manipulation occurs in a Angular client.
My Http reponse:
[
{
"function":"nameA",
"rast":"F1",
"tag":"EE",
"probs":"0,987"
},
{
"function":"nameB",
"rast":"F2",
"tag":"SE",
"probs":"0,852"
},
{
"function":"nameC",
"rast":"F3",
"tag":"CE",
"probs":"0,754"
}
]
I need convert the result in json bellow to post in next request:
[
{
"function":"nameA",
"rast":"F1",
"type":{
"name":"EE"
},
"id":"alpha"
},
{
"function":"nameB",
"rast":"F2",
"type":{
"name":"SE"
},
"id":"alpha"
},
{
"function":"nameC",
"rast":"F3",
"type":{
"name":"CE"
},
"id":"alpha"
}
]
I filter the response with this JSON Extractor:
[*].["function", "rast", "tag"]
But now I need to solve other problems:
Add an id attribute (same for all functions)
Add an object with the name type.
Move the tag attribute into the object called type.
Rename the tag attribute to the name.
Add JSR223 PostProcessor as a child of the request which returns the original JSON
Put the following code into "Script" area:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData()).each { entry ->
entry << [type: [name: entry.get('tag')]]
entry.remove('tag')
entry.remove('probs')
entry.put('id', 'alpha')
}
def newJson = new groovy.json.JsonBuilder(json).toPrettyString()
log.info(newJson)
That's it, you should see the generated JSON in jmeter.log file.
If you need to have it in a JMeter Variable add the next line to the end of your script:
vars.put('newJson', newJson)
and you will be able to access generated value as ${newJson} where required
More information:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
what is the best approach to capture from the following array?
i only need to capture the value of ANY 'beginDate', e.g: 2017-05-01T08:30:00 could be a valid one in below example
i need to make sure the 'beschikbaar' = TRUE for the date that i'm capturing
i tried using json path extractor with similar lines: $..[?(#.beschikbaar == 'true')].beginDate but i'm facing syntax errors that i cant fix due to my limited regex/json path knowledge
the example array is;
{
"data":
[
[
{
"beginDate":"2017-05-01T08:00:00",
"eindDate":null,
"beschikbaar":false
},
{
"beginDate":"2017-05-01T08:15:00",
"eindDate":null,
"beschikbaar":false
},
{
"beginDate":"2017-05-01T08:30:00",
"eindDate":"2017-05-01T10:30:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T08:45:00",
"eindDate":"2017-05-01T10:45:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:00:00",
"eindDate":"2017-05-01T11:00:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:15:00",
"eindDate":"2017-05-01T11:15:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:30:00",
"eindDate":"2017-05-01T11:30:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T09:45:00",
"eindDate":"2017-05-01T11:45:00+02:00",
"beschikbaar":true
},
{
"beginDate":"2017-05-01T10:00:00",
"eindDate":"2017-05-01T12:00:00+02:00",
"beschikbaar":true
},
Don't use regular expressions for JSON data, JMeter provides JSON Extractor designed to work with JSON data via JSON Path Language so you should be able to get your "beginDate" with the query like:
$..[?(#.beschikbaar == true)].beginDate
Demo:
Check out JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios article for more detailed explanation and few more examples.
You can try this
(?s)\{.*?\"beginDate\":\"([^{]*?)\"[^{]+\"beschikbaar\":true.*?\}
(?s) is single-line modifier which makes . match the line break
You can test it at http://www.regexplanet.com/advanced/java/index.html
And set Template to $1$ means using the first group
I have a simple json file which is :
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":35,"target":44,"value":1},
{"source":44,"target":35,"value":1},
{"source":45,"target":35,"value":1},
{"source":45,"target":44,"value":1},
{"source":35,"target":49,"value":1},
{"source":49,"target":35,"value":1}
]
}
when I save it use exactly the html code as shown in http://bl.ocks.org/4062045#index.html and address the above json, nothing appears on the cancas.
I appreciate it if you help me with this one as I am not very familiar with it. Moreover, it would be great if I know the minimum code required for drawing a graph like this using json.
Best,
The number of "source" and "target" refer to the index of the item in nodes array.
So you can change your json to following:
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":0,"target":1,"value":1},
{"source":1,"target":2,"value":1},
{"source":2,"target":3,"value":1},
{"source":3,"target":4,"value":1},
]
}
Then you can just copy the codes from http://bl.ocks.org/4062045#index.html example as the minimum code.
Remenber to change the json file to your own json file.
d3.json("path/to/your/json", function(error, graph) {
//codes
});