parse json array in postgresql 9.3 - json

I want to write a function that takes a json array as input parameter and then to parse the array and store each json element in a text variable to use later.
I am using Postgres 9.3 and using json_array_elements() to help but cant go further.

Related

What is the best way to store JSON in Postgres

What is the best way to store JSON data in Postgres? Is it possible to query a stored json string without using like/regex?
The best option is to use a column defined as jsonb.
And yes, Postgres has a lot of functions that let you query the content of a JSON value.
e.g. to check if a key/value pair is contained in the value json_column #> '{"key": "value"}'

Converting a string of an array into an array in JS

I have a project I'm working on that implements MySQL, React and Express.js. I need to save an array into MySQL, but there are currently no ways to save an array, as far as I could see, so I was forced to convert it into a string. When I get it back from Express to the client, it's obviously a string, so I can't access the data. This array is used for a graph, mainly. What are some of the ways I can convert this string back to an array?
You can use JSON.parse() to convert a string into an array.
const response = "[1,2,3]";
console.log(JSON.parse(response));
You can store your json object (including arrays )in form of text in mysql database.What you have to do is JSON.stringify("your array") and persist it in database.And while you are retrieving it back from database you can JSON.parse() to get it in form of JavaScript object
Depends on how you formed the string. If you used , for joining the elements, then you can use javascript's string.split() method.
let str = '1,2,3,4';
let arr = str.split(',');
Just pass in whatever delimiter you used to join the elements.
OR
If you're saving elements as a json string, then use JSON.parse(str) as shown by Nils Kähler in his answer

Convert doctrine array to JSON

Is there a way to read a column of doctrine type "simply_array" or "array" in json?
My doctrine database is approached from another api and I want to read data from that api. However there is a column of type doctrine array that I want to convert into JSON.
I am unsure if there is a preferred way of doing this or I need to hack my way around it.
Here is an example of what is stored in the database as a doctrine array:
"a:1:{i:0;a:3:{s:3:\u0022day\u0022;i:5;s:4:\u0022time\u0022;s:7:\u0022morning\u0022;s:12:\u0022availability\u0022;N;}}"
That looks like the format of PHP's serialize() function. And the literal double-quotes in the string have been converted to unicode escape sequences.
You could do the following:
Fetch the serialized string
Fix the \u0022 sequences (replace them with ")
unserialize() it to reproduce the array
Convert the array to JSON with json_encode().

Create json array of records with Delphi

I'm trying to create a json with multiple records by following this example: Generate a sample JSON with an array in it in Delphi XE5
must be the same way, except that when I add the array to the object
JSonObj.AddPair (TJSONPair.Create ('records', TJSONArray));
returns the error:
"There is the overloaded version of 'Create' that can be called with arguments These"
How do I add to the array object?
If I convert an array to string and add, to receive the amounts can not treat as an array ...
You're passing it the class reference for a JSON array. You need to pass it an instance.
arr := TJSONArray.Create;
JSONObj.AddPair(TJSONPair.Create('records', arr));
Look carefully at the answers in the question you link to, and you'll see this is exactly what they're doing, too.

Convert json String to object when square brackets are missing in java

I am trying to parse a json string mentioned below,
"inventoryItems":{"fare":"299.00","ladiesSeat":"false","passenger":{"address":"xxx,Bangalore","age":"26","email":"xxxxxxxxx#gmial.com","gender":"MALE","idNumber":"123ABC","idType":"PAN_CARD","mobile":"9999999999","name":"abcd","primary":"true","title":"Mr"},"seatName":"27"}
The structure is there is an inventoryItems which is an array and inside that there is an passenger *array*, The problem is there are no square braces, since the array contains only one value in both inventoryItems and passenger. This malformed data is from a third party server so I cant correct them. While converting this to Object Jackson library is throwing an excception.
My question is how to form a proper json string with square braces from the above so that jackson is able to convert it into java object?
The DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY should solve your problem (via [ObjectMapper.enable(...)) without a conversion of the string.