Converting a JSON body into a REST URI - json

can someone please advise how can I convert this JSON body into a REST URI ?
GET api/_search
{
"age":"5",
"aggs" : {
"uniq_gender" : {
"terms" : { "field" : "Gender.keyword" }
}
}
}

You may proceed with one of two options:
Use POST with body
POST api/_search
{
"age":"5",
"aggs" : {
"uniq_gender" : {
"terms" : { "field" : "Gender.keyword" }
}
}
}
It may seem like a hack, but it is simple and frankly it is widely used. Basically from REST perspective it may be considered as resource creation (filter rather than seach might be a better word here).
Use query string with GET.
Something like:
GET api/_search?age=5,field=Gender.keyword
The problem with using query string is that it may be limited. In RFC there is a code for such a case. For example IE browser has such a limit - see details.
Generally speaking if there is no technical problem, readability issue may appear - it is hard to deal with 1000+ symbols string.

Related

JSON structure for message payload

I am sending a message to a third party and the payload looks like as below:
{
"keys": {
"salesCaseId": 1000449
},
"attributes": {
"assetDeliveryDetails": {
"registrationNumber": "PXS3388",
"vinNumber": "DW1265",
"deliveryDate": "16-04-2020"
}
}
}
"Keys" and "attributes" are common for all the messages between us.
Question: Because I have only one nested JSON inside attributes which is assetDeliveryDetails is it necessary to have this nested JSON property? or I can have the other 3 attributes one level higher as below :
{
"keys": {
"salesCaseId": 1000449
},
"attributes": {
"assetRegistrationNumber": "PXS3388",
"assetVinNumber": "DW1265",
"assetDeliveryDate": "16-04-2020"
}
}
Does it makes sense to group because I can logically group them together or is it an overhead because I need to create equivalent DTO classes as well?
Generally speaking you want any data structure (whether that's JSON, XML, an Object model, etc.) to be as simple as possible to fulfill your requirements. If the second model (i.e. without assetDeliveryDetails) fulfills your requirements then I recommend you go with that. Unnecessary complexity is not desirable as it will be more difficult to understand and take more resources to parse..

Vuelidate "OR" validator example

OK - searched for quite a while all over and cannot find an example or using the OR validator from Vuelidate. The one mentioned in this github issue is not valid and the issue resolves without providing a valid OR example. The docs mention it exists but do not have any example that we could find. Anyone have or know of an valid example?
If you want a field that take multiple validation types, like signing in with a username or email address, you can do something like this
data () {
return { {
multiusername: '',
}
},
validations: {
multiusername: {
required,
mycustomval: or(email, alphaNum)
}
}

JSON Deserialization on Talend

Trying to figuring out how to deserialize this kind of json in talend components :
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
"ryan.buckley#toofr.com": {
"confidence":18,"email":"ryan.buckley#toofr.com","default":16
},
"ryanbuckley#toofr.com": {
"confidence":17,"email":"ryanbuckley#toofr.com","default":17
},
"ryan_buckley#toofr.com": {
"confidence":16,"email":"ryan_buckley#toofr.com","default":18
},
"ryan-buckley#toofr.com": {
"confidence":15,"email":"ryan-buckley#toofr.com","default":19
},
"ryanb#toofr.com": {
"confidence":14,"email":"ryanb#toofr.com","default":14
},
"buckley#toofr.com": {
"confidence":13,"email":"buckley#toofr.com","default":13
}
}
This JSON comes from the Toofr API where documentation can be found here .
Here the actual sitation :
For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.
Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?
Thank you in advance !
EDIT 1:
Here's my tExtractJSONfields :
When using tExtractJSONFields with XPath, you need
1) a valid XPath loop point
2) valid XPath mapping to your structure relative to the loop path
Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
but this structure would be valid:
{
"contact": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"contact": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
So with the correct data the loop point might be /contact.
Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.
EDIT
JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com
The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.
I have been ingesting some complex json structures and did this via minimal json libraries, and tjava components within talend.

Is it ok to have JSON objects with varying number of attributes?

I am designing an API resonse for my mobile app right now, and response should contain array of operations. Some of them might have all attributes, some may not, see the example:
{
"operations":[
{
"type":"0",
"location":"01"
},
{
"type":"1",
"location":"1234"
"item_id":"",
"item_name":"Item A",
}
]
}
Is that a good way, or should I reconsider my design? I mean the varying number of attribues. Thank you!
Although It will be good for the bandwidth to keep the attributes out of the json string that doesn't have values. But I will suggest you to keep it other way, either send null or empty string "" it will be help at the decoding side
{
"operations":[
{
"type":"0",
"location":"01"
"item_id":null,
"item_name":null,
},
{
"type":"1",
"location":"1234"
"item_id":"",
"item_name":"Item A",
}
]
}
It depends on the code you'll be writing to handle the object :) As long as you write your code to handle missing elements you'll be fine.
Javascript couldn't give a hoot if an object in an array matches the structure of the other objects or not, if that's what you're concerned about.
p.s: Watch those comma's! They cause me more grief than anything else :p IE will break on a trailing comma :(

Display JSON as HTML [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Any recommendations on how to embed JSON in an HTML page with the JSON formatted in a human readable style? For example, when you view XML in a browser, most browsers display the XML formatted (indented, proper line breaks, etc). I'd like the same end result for JSON.
Color syntax highlighting would be a bonus.
Thanks
You can use the JSON.stringify function with unformatted JSON. It outputs it in a formatted way.
JSON.stringify({ foo: "sample", bar: "sample" }, null, 4)
This turns
{ "foo": "sample", "bar": "sample" }
into
{
"foo": "sample",
"bar": "sample"
}
Now the data is a readable format you can use the Google Code Prettify script as suggested by #A. Levy to colour code it.
It is worth adding that IE7 and older browsers do not support the JSON.stringify method.
If you are deliberately displaying it for the end user, wrap the JSON text in <PRE> and <CODE> tags, e.g.:
<html>
<body>
<pre>
<code>
[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
{
color: "magenta",
value: "#f0f"
},
{
color: "yellow",
value: "#ff0"
},
{
color: "black",
value: "#000"
}
]
</code>
</pre>
</body>
</html>
Otherwise I would use JSON Viewer.
For the syntax highlighting, use code prettify. I believe this is what StackOverflow uses for its code highlighting.
Wrap your formatted JSON in code blocks and give them the "prettyprint" class.
Include prettify.js in your page.
Make sure your document's body tag calls prettyPrint() when it loads
You will have syntax highlighted JSON in the format you have laid out in your page. See here for an example. So if you had a code block like this:
<code class="prettyprint">
var jsonObj = {
"height" : 6.2,
"width" : 7.3,
"length" : 9.1,
"color" : {
"r" : 255,
"g" : 200,
"b" : 10
}
}
</code>
It would look like this:
var jsonObj = {
"height" : 6.2,
"width" : 7.3,
"length" : 9.1,
"color" : {
"r" : 255,
"g" : 200,
"b" : 10
}
}
This doesn't help with the indenting, but the other answers seem to be addressing that.
I think you meant something like this:
JSON Visualization
Don't know if you might use it, but you might ask the author.
something like this ??
pretty-json
https://github.com/warfares/pretty-json
live sample:
http://warfares.github.io/pretty-json/
Here's a light-weight solution, doing only what OP asked, including highlighting but nothing else: How can I pretty-print JSON using JavaScript?
Could use JSON2HTML to transform it to nicely formatted HTML list .. may be a little more powerful than you really need though
http://json2html.com
If you're just looking to do this from a debugging standpoint, you can use a Firefox plugin such as JSONovich to view the JSON content.
The new version of Firefox that is currently in beta is slated to natively support this (much like XML)
Here's a javasript tool that will convert JSON to XML and vice versa, which should enhance its readability. You could then create a style sheet to color it or do a complete transform to HTML.
http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
Your best bet is going to be using your back-end language's tools for this. What language are you using? For Ruby, try json_printer.
SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript. To get an idea of what SyntaxHighlighter is capable of, have a look at the demo page.
First take the JSON string and make real objects out of it. Loop though all of the properties of the object, placing the items in an unordered list. Every time you get to a new object, make a new list.