I'm generating some JSON with XStream for an object that contains an ArrayList. I have the #XStreamImplicit annotation set for the specific field. When I have 2 elements in the array it converts to JSON properly...
"cap": [
"switch",
"switch2"
]
However if there is only one element in the array I get this...
"cap": "switch"
Therefore it's not showing up as an array (even though there is only one element in it). This makes it a challenge to deserialize it on the other end (which I do not have control of). What I want it to do is the following...
"cap": [
"switch"
]
I'm assuming this is some sort of automatic assumption made for efficiency sake in the library. Either in XStream or Jettison. However is there a way to force the array setting here?
Related
I use JSON Schema to describe my data structure
My data contains specific arrays of the following structure:
{
"string1",
"string2",
...
"stringN",
{
"object": "as",
"last": "item"
}
}
I'm wondering how to describe this in json schema (especially the thing "last item is an object")
If I knew the number of string items, "prefixItems" would do the thing (but there could be any number of them).
If the object was the first (not last) item, "prefixItems" together with "items" would work.
If is use "contains", it only checks the object is somewhere in my array, not checking it is the last item.
Seems that I need something like "reversePrefixItems", if such option existed - but it doesn't exists.
So, what is a proper way to describe the last item of an array? (and optionally all the preceding ones - knowing their type but not their total count)
there has been discussion of a proposed keyword postfixItems but it has not yet made it into the spec. I don't think there is a way to do this currently.
I am getting data from a powershell script from an automation account. i use this data to create reports and alerts. in 90% of the cases it is an json array.
So in my logic app i am going to parse the json and filter the array afterwards. i use the filtered data to create alerts based of the "id" in the filter. As i said in 90% of the cases this works perfectly.
But, if i only get one object from the script instead of the array i was getting the error:
"message": "Invalid type. Expected Array but got Object."
i solved this on the Parse Json step with:
"type": [
"object",
"array"
]
But now my "filter Array "is obviously not working anymore:
BadRequest. The 'from' property value in the 'query' action inputs is of type 'Object'. The value must be an array.
So i am trying to figure out how to solve this, is it possible to make an if/else statement and check if its an array or object?
Or should i initialize an empty array variable and append the object into the array so the filter will still work?
In the 'From' parameter of your 'Filter array' action, try using the array function to convert the object from your JSON into array.
I'm using OpenRefine to pull in information on publisher policies using the Sherpa Romeo API (Sherpa Romeo is a site that aggregates publisher policies). I've got that.
Now I need to parse the returned JSON so that those with certain pieces of information remain. The results I'm interested in need to include the following:
'any_website',
'any_repository',
'institutional_repository',
'non_commercial_institutional_repository',
'non_commercial_repository'
These pieces on information all fall under an array called "permitted_oa". For some reason, I can't even work out how to just pull out that array. I've tried writing grel expressions such as
value.parseJson().items.permitted_oa
but it never reutrns anything.
I wish I could share the JSON but it's too big.
I can see a couple of issues here.
Firstly the Sherpa API response items is an array (i.e. a list of things). When you have an array in the JSON, you either have to select a particular item from the array, or you have to explicitly work through the list of things in the array (aka iterate across the array) in your GREL. If you've previously worked with arrays in GREL you'll be familiar with this, but if you haven't
value.parseJson().items[0] -> first item in the array
value.parseJson().items[1] -> second item in the array
value.parseJson().items[2] -> third item in the array etc. etc.
If you know there is only ever going to be a single item in the array then you can safely use value.parseJson().items[0]
However, if you don't know how many items will be in the array and you are interested in them all, you will have to iterate over the array using a GREL control such as "forEach":
forEach(value.parseJson().items, v, v)
is a way of iterating over the array - each time the GREL finds an item in the array, it will assign it to a variable "v" and then you can do a further operation on that value using "v" as you would usually use "value" (see https://docs.openrefine.org/manual/grel#foreache1-v-e2 for an example of using forEach on an array)
Another possibility is to use join on the array. This will join all the things in an array into a string.
value.parseJson().items.join("|")
It looks like the Sherpa JSON uses Arrays liberally so you may find more arrays you have to deal with to get to the values you want.
Secondly, in the JSON you pasted "oa_permitted" isn't directly in the "item" but in another array called "publisher_policy" - so you'll need to navigate that as well. So:
value.parseJson().items[0].publisher_policy[0].permitted_oa[0]
would get you the first permitted_oa object in the first publisher_policy in the first item in the items array. If you wanted to (for example) get a list of locations from the JSON you have pasted you could use:
value.parseJson().items[0].publisher_policy[0].permitted_oa[0].location.location.join("|")
Which will give you a pipe ("|") separated list of locations based on the assumption there is only a single item, single publisher_policy and singe permitted_oa - which is true in the case of the JSON you've supplied here (but might not always be true)
I am working in VB.net (4.5) and using the Newtonsoft Json Linq package.
I have an array (BuyList) composed of 100 market orders in the format:
{{
"Quantity": 0.14333804,
"Rate": 6693.01
}}
I would like to sort the array by "Rate" values from low to high.
Here is how I am creating the array:
Dim BuyList As Array = BittrexResponse("result")("buy").Children().ToArray()
BittrexResponse is a JObject created from parsing an Http request.
I have been trying to use:
Array.sort(BuyList)
Which throws an error stating failed to compare two elements in the array and that at least one element must implement IComparable. How do I specify that I want to compare all "Rate" values and sort them from low to high?
You sort an array of any type in exactly the same way. If the elements don't implement IComparable the the Sort method has no idea how to compare them in order to sort them, so you have to tell it. How would you normally get that Rate value from such an object and what type is it? I'm guessing that you'd use the Item property and that it is either Double or Decimal. You need to tell the Sort method that, e.g.
Array.sort(BuyList, Function(jo1, jo2) CDec(jo1.Item("Rate")).CompareTo(CDec(jo2.Item("Rate"))))
That's using the overload that takes a Comparison(Of T) delegate. Such a delegate refers to a method that, given two array elements, will compare them in a specific way and return an Integer value that indicates their relative order. That usually comes down to calling one or more CompareTo methods of types that do implement IComparable, as Decimal does.
I'm trying to retrieve Firebase data with Polymer Fire. When I look in the console it's returning two objects, however the length of the array is three. When I'm trying to execute a dom-repeat I'm successfully printing two filled in rows but also one empty row. How is this possible?
Firebase stores data as associative arrays, essentially a dictionary of key/value pairs.
That means that in order to deal with arrays, it converts an array to a dictionary when you store it and then back to an actual array when you read it. Here you are getting bitten by the SDK converting your non-array into an array by padding it with a leading element.
If you don't want the SDK to do this conversion, the easiest way is to store the items with a non-numeric key, e.g. "item1", "item2".
Read more about how Firebase deals with arrays in this classic blog post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html