How to format post body as json? - json

I'm trying to mimic a post request, using python requests.
The post body is
nonce=b3272d453ca8734f8df1c78ce201f00c&from=01%2F12%2F22&to=31%2F12%2F22&columns%5B%5D=Transaction.DateTimeConverted&columns%5B%5D=Terminal.Id
I have managed to create
{"nonce": "b3272d453ca8734f8df1c78ce201f00c",
"from": "01/12/22",
"to": "31/12/22",
"columns"[]: "Transaction.DateTimeConverted"
"columns"[]: "Terminal.Id"
}
But something isn't working, I suspect it is around my misunderstanding of percentage enncoding representation in Json, aroud the columns? Can someone help?

"key"[] : "value1", "key"[]: "value2" is not valid JSON. you would need to convert it to "key": [ "value1", "value2" ].

Related

Convert JSON array and keys to JSON dict

I've noticed that some APIs use a format of sending a stripped down version of their data via a JSON array like the following:
[
"joe",
[
5,
2,
"yellow"
]
]
And store a set of keys like the following:
[
"name",
["some_data", [
"favorite_number",
"least_favorite_number",
"car_color"
]]
]
To turn the data from a bunch of random values to a readable set of data, like the following:
{
"name": "joe",
"some_data": {
"favorite_number": 5,
"least_favorite_number": 2,
"car_color": "yellow"
}
}
I was wondering how this could be done? I'd prefer it'd be in python, but I'm fine with programming my own libraries.
After grasping at more straws than I could fit in my mouth, I've figured it out. JSON schema is what I'm supposed to be using!

Suppress datamember attribute name JSON

I have an issue serializing to JSON moving from an IList<"string"> to an IList<"customobject">. The endpoint is expecting an array of strings such as :-
"options": [
"foo1",
"foo2"
]
With the customobject I am getting the following :-
"options": [
{
"name": "foo1"
},
{
"name": "foo2"
}
]
Is there any way to suppress name attribute and continue to get an array of strings with WCF, or do I have to do it another way? Any help would be much appreciated.
Would still appreciate any insight into the possibility of doing this, but for now has been resolved by adding the option as a string as well as an object, and only exporting the string. I know this is duplication, but will have to wait for an update to the endpoint.

Get keys in JSON

I get the following JSON result from an external system:
{
"key1": "val1",
"key2": "val2",
"key3": "val3"
}
Now I want to display all keys and all values by using JSONPath. So I am looking for something to get key1, key2 and key3 as a result. Additionally I would like to use the index of a property, e. g. $....[2].key to get "key3" etc.
Is there a way to do something like this?
I found that the tilda ~ symbol is able to retrieve the keys of the values it's called upon. So for your example a query like this:
$.*~
Returns this:
[
"key1",
"key2",
"key3"
]
Another example, if we had a JSON document like this:
{
"key1": "val1",
"key2": "val2",
"key3": {
"key31":"val31",
"key32":"val32"
}
}
A query like this:
$.key3.*~
Would return this:
[
"key31",
"key32"
]
It's important to note that these examples work on JSONPath.com and some other simulators/online tools, but on some they don't. It might come from the fact that I found out about the tilda(~) operator in the JSONPath plus documentation and not the official one.

multiple timestamps in json body

I'm trying to send time stamped key value pairs to the ThingsBoard Demo platform (demo.thingsboard.io). The standard way is to send a timestamp and with some key-value-pairs like so:
{"ts":1451649600512, "values":{"key1":"value1", "key2":"value2"}}
My problem is, that I need to process up to 100 acceleration measurements per second and I dont want to send a http post for every x-y-z value-package. Is there a way to send one json body with, lets say, 100 timestamps with corresponding measurements?
I tried it:
{
"ts": 1508695100,
"values": {
"key1": 34,
"key2": 26
},
"ts": 1508695200,
"values": {
"key1": 38,
"key2": 29
}
}
There is no error message when pushing this json to ThingsBoard with curl, but only the last timestamp-value-block seems to be recognized by ThingsBoard.
Any suggestions on how to to solve my problem?
You should use following format (json array):
[{"ts":1451649600512, "values":{"key1":"value1", "key2":"value2"}}, {"ts":1451649600513, "values":{"key1":"value1", "key2":"value2"}}]
or
[
{
"ts":1451649600512,
"values":{
"key1":"value1",
"key2":"value2"
}
},
{
"ts":1451649600513,
"values":{
"key1":"value1",
"key2":"value2"
}
}
]
BTW, the JSON you have tried is not a valid JSON document at all. Please check the validity of the document before sending.

Is it problematic to have a JSON Lines file that has mixed JSON structures?

I would like to know whether, if a JSON Lines file is structured like this:
{"structure1":"some kind of file header"}
{"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]
{"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]
{"structure3":"maybe some kind of file footer"}
Is it considered a non-valid JSONLines format? I looked at the standard at http://jsonlines.org/ and I couldn't see anything one way or the other.
Thank you.
Your lines are each one valid ( but missing a '}' and the end of the second and third).
but if you want to put them all in one file it will be not valid. They have to be in array and separated with , or an object with key/value ( where the value can be array or object )
example of array :
[
{"structure1":"some kind of file header"},
{"structure2": [ {"key1":"aaa", "key2":"bbb"}, {"key1":"one", "key2":"two"}]},
{"structure2": [ {"key1":"xxx", "key2":"yyy"}, {"key1":"first", "key2":"second"}]},
{"structure3":"maybe some kind of file footer"}
]
or an object :
{
"structure1": "some kind of file header",
"structure2": [{
"key1": "aaa",
"key2": "bbb"
}, {
"key1": "one",
"key2": "two"
}],
"structure2": [{
"key1": "xxx",
"key2": "yyy"
}, {
"key1": "first",
"key2": "second"
}],
"structure3": "maybe some kind of file footer"
}
You can test your json on this site to see if valid or not :
http://www.jslint.com/ or http://jsonlint.com/