Converting string with new lines into json array - arrays

I have the following string(with new lines after each value):
"
value1
value2
value3
"
I need to convert it to a json array(using python) in the following format:
{"keywords": [{"keyword": "value1"},{"keyword": "value2"},{"keyword": "value3"}]}
I tried all kinds of methods, always resulting in invalid json.
Any suggestions?

Split the string into an array, map it to a dictionary of keyword:line, and convert it into json.
import json
def map_line(line):
return {"keyword": line}
lines = "value1\nvalue2\nvalue3".split("\n")
result = json.dumps({"keywords": list(map(map_line, lines))})

import json
sample_data = '{"keywords": [{"keyword": "value1"},{"keyword": "value2"},{"keyword": "value3"}]}'
for idx, key in enumerate(json.loads(sample_data)):
for idx2, key2 in enumerate(json.loads(sample_data)[key]):
print(key2)
Output:
{'keyword': 'value1'}
{'keyword': 'value2'}
{'keyword': 'value3'}

Related

Groovy iterate over Arraylist of Maps

I'm working in a declarative pipeline, and I have a string that looks like this:
'[[key_A:value1, key_B:value2, key_C:value3],[key_A:value4, key_B:value5, key_C:value6],[key_A:value7, key_B:value8, key_C:value9]]'
Can I get help on what the quickest way is to turn the string into a map, then retrieve values of each map in the arraylist?
Can I get help on what the quickest way is to turn the string into a
map, then retrieve values of each map in the arraylist?
The input string you provide doesn't look like a map, it looks like a list of map. You can turn the string into a list of map using something like this (notice that the values here are quoted so they are strings, otherwise you would have to provide variables for value1, value2 etc):
def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]'
def inputList = Eval.me (inputString)
Then you could iterate over that list to retrieve the maps and do whatever you want to do with the values in the maps:
def inputString = '[[key_A:"value1", key_B:"value2", key_C:"value3"],[key_A:"value4", key_B:"value5", key_C:"value6"],[key_A:"value7", key_B:"value8", key_C:"value9"]]'
def inputList = Eval.me (inputString)
inputList.each { Map m ->
println m.values()
}

How to get number of elements in an array in JSON using Groovy script?

I have a simple json file for an array called "sample.json"
content of sample json:
{"apps": ["app1","app3","app3"]}
How I can get the size of the array on this JSON file using groovy script. I tried following but no luck,
def json_file = readFile("sample.json")
def json_output = readJSON text: json_file
def json = new JsonSlurper().parseText(json_output)
println json.results.size('apps')
Can anyone help me to get the result as size of the array is "3" ?
Thanks.
You don't need JsonSlurper in pipeline.
readJson already returns a parsed json.
This should work:
def json_file = readFile("sample.json")
def json = readJSON text: json_file
println json.apps.size()

Filter json array data in spark dataframe

I have spark dataframe that I am converting into JSON format:
json = df.toJSON().collect()
print(json)
['{"lot_number":"4f19-9deb-0ef861c1a6a1","recipients":[{"account":"45678765457876545678","code":"user1","status":"pending"},{"account":"12354567897545678","code":"error2","status":"pending"}]}',
'{"lot_number":"09ad-451e-8fb1-50bc185ef02f","recipients":[{"account":"4567654567876545678","code":"user3","status":"pending"},{"account":"12354567876545678","code":"user2","status":"pending"}]}']
I need to filter the data from array, that is all recipients whose code is "user1".
I'm expecting this result:
['{"lot_number":"4f19-9deb-0ef861c1a6a1","recipients":[{"account":"45678765457876545678","code":"user1","status":"pending"}'
]
Can anyone help to filter the data as shown above?
Firstly you will need to convert the string in the list to dict objects.
import json
json_rdd = df.toJSON().collect()
json_ls = [json.loads(x) for x in json_rdd]
# Now you can filter using "user1"
final_json_ls = [x for x in json_ls if x.get("recipients")[0].get("code") == "user1"]
If you have multiple recipients:
new_list = list()
for lot in json_ls:
recs = lot.get('recipients')
lot_recipients = [rec for rec in recs if rec.get("code") == "user1"]
if lot_recipients:
new_list.append({"lot_number": lot.get('lot_number'),
"recipients": lot_recipients})
# OUTPUT
# [{'lot_number': u'4f19-9deb-0ef861c1a6a1', 'recipients': [{u'status': u'pending', u'account': u'45678765457876545678', u'code': u'user1'}]}]
And since you want to convert it back to json to send POST requests:
for ls in new_list:
lot = ls.get("lot_number")
url = "test.com/api/v1/notify/request/"+ batch
response = requests.put(url, data=item, headers=headers)
print(response.text)

Ruby: convert string to array

a = IO.readlines('uniqID.txt')
puts id = a[0]
id = ["Gh089k" , "HG987"] #getting value from txt file
id.class #String
id.push("GD977")
How to convert the above string into array. so that I use method like push. Here id is string looks like an array
Error: undefined method `push' for "[\"Gh089k\", \"HG987\"]":String (NoMethodError)
It looks like a JSON string. You can parse json string to get the desired result.
require 'json'
JSON.parse("[\"Gh089k\", \"HG987\"]") # => ["Gh089k", "HG987"]
Here:
id = JSON.parse(a[0])
Hope it helps !

Scala play api for JSON - getting Array of some case class from stringified JSON?

From our code, we call some service and get back stringified JSON as a result. The stringified JSON is of an array of "SomeItem", which just has four fields in it - 3 Longs and 1 String
Ex:
[
{"id":33,"count":40000,"someOtherCount":0,"someString":"stuffHere"},
{"id":35,"count":23000,"someOtherCount":0,"someString":"blah"},
...
]
I've been using the play API to read values out using implicit Writes / Reads. But I'm having trouble getting it to work for Arrays.
For example, I've been try to parse the value out of the response, and then convert it to the SomeItem case class array, but it's failing:
val sanityCheckValue: JsValue: Json.parse(response.body)
val Array[SomeItem] = Json.fromJson(sanityCheckValue)
I have
implicit val someItemReads = Json.reads[SomeItem]
But it looks like it's not working. I've tried to set up a Json.reads[Array[SomeItem]] as well, but no luck.
Should this be working? Any tips on how to get this to work?
import play.api.libs.json._
case class SomeItem(id: Long, count: Long, someOtherCount: Long, someString: String)
object SomeItem {
implicit val format = Json.format[SomeItem]
}
object PlayJson {
def main(args: Array[String]): Unit = {
val strJson =
"""
|[
| {"id":33,"count":40000,"someOtherCount":0,"someString":"stuffHere"},
| {"id":35,"count":23000,"someOtherCount":0,"someString":"blah"}
|]
""".stripMargin
val listOfSomeItems: Array[SomeItem] = Json.parse(strJson).as[Array[SomeItem]]
listOfSomeItems.foreach(println)
}
}

Resources