# Process multiple occurance of same field in JSON with different values

**URL:** <https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926>\
**Category:** KNIME Analytics Platform\
**Tags:** json\
**Created:** [July 26, 2021, 9:39pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926 "2021-07-26T21:39:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rumanac](https://forum-cdn.knime.com/user_avatar/forum.knime.com/rumanac/32/26597_2.png) [@rumanac](https://forum.knime.com/u/rumanac)\
**Post date:** [July 26, 2021, 9:39pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/1 "2021-07-26T21:39:41Z")

</div>

Hi, I have a json that looks similar to the record below. In the json, it can have multiple GIVENNAME, MIDDLENAME, and SURNAME for the same record. I need to extract all of the SURNAME, for example, in one field called FirstName with space between them as they all belong to a single record. I tried JSON Path, but not sure how to get what I am looking for here. Any help will be highly appreciated.  
[{  
“string”: “May”,  
“termType”: “GIVENNAME”  
}, {  
“string”: “Green”,  
“termType”: “MIDDLENAME”  
}, {  
“string”: “Dowman”,  
“termType”: “SURNAME”  
}, {  
“string”: “Hinna”,  
“termType”: “SURNAME”  
}]

The output I am looking for is:  
FirstName, MiddleName, LastName  
May, Green, Dowman Hinna

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [July 27, 2021, 2:38am UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/2 "2021-07-27T02:38:29Z")

</div>

Hi @rumanac , this is quite simple to do actually.

Just to confirm what you said: “I need to extract all of the SURNAME, for example, in one field called FirstName with space between them as they all belong to a single record”. I am guessing that you meant LastName instead of FirstName, and I put something quickly for you based on that assumption.

This is what the workflow looks like:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/6/a/6a5f152bd95b18f8385ab00090b74e0dbe6d3469.png)

Input data (same as what you have):  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/f/0/f0df413a4a6b0b3fde17f8b85a3d361002a092ff.png)

Results:  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/e/7/e7349ebf0574074e074b5b0cb34bfe59beced998.png)

FYI, the rule that was applied on SURNAME, that is if the termType repeats, the string values for the same termType will be joined by a space, is applied on all 3 termType.

Here’s my quick workflow:  
[Process multiple occurance of same field in JSON with different values.knwf](https://forum.knime.com/uploads/short-url/z9SaXcvN2HUqOAZA3nRz3ELuSPU.knwf) (23.5 KB)

BTW, this is quite a weird JSON structure. Normally, the structure is like:  
`"variable" : "value"`

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [July 27, 2021, 2:48am UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/3 "2021-07-27T02:48:58Z")

</div>

Hi @rumanac , just some additional comments. JSON Path would not be useful here, because of the fact that the JSON data that you have is not following the structure of `"variable" : "value"` that I have already mentioned.

You use JSON Path to go fetch the value of a variable. In your case:

1. The variables are “string” and “termType”
2. It looks like you cannot determine in advance how many “string” and “termType” you can have, since you can have more than 1 GIVENNAME, and/or more than 1 MIDDLENAME, and/or more than 1 SURNAME

---

<div class="post-metadata">

**Author:** ![rumanac](https://forum-cdn.knime.com/user_avatar/forum.knime.com/rumanac/32/26597_2.png) [@rumanac](https://forum.knime.com/u/rumanac)\
**Post date:** [July 27, 2021, 3:39am UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/4 "2021-07-27T03:39:14Z")

</div>

Hi Bruno,  
Nice to hear from you. Hope you are doing well. Thank you so much for the response. That looks like a good plan of getting the expected output. I will definitely try it out. I actually ended up completing it using a different approach - I wrote some Java snippets to get the expected output. Thanks again! Rumana

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [July 27, 2021, 1:06pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/5 "2021-07-27T13:06:19Z")

</div>

Hi @rumanac , no problem, happy to help, and happy to hear that you resolved it. As we always say, there are more than 1 way to resolve something in Knime, and this can definitely be done in Java or Python.

It would be interesting if you could share your Java Snippets solution so that the other members of the Community can see another way to do this.

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [July 27, 2021, 4:48pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/6 "2021-07-27T16:48:02Z")

</div>

Json Path would be a great solution if there was some id attached. I would like to see the full data to try out some possibilites

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [July 27, 2021, 6:35pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/7 "2021-07-27T18:35:38Z")

</div>

Hi @Daniel_Weikert , I would be interested to see how we can use JSON Path here.

If the data was like this:

```auto
{
	"GIVENNAME": [
		"May"
	],
	"MIDDLENAME": [
		"Green"
	],
	"SURNAME": [
		"Dowman",
		"Hinna"
	]
}

```

Then for me it would make sense to use JSON Path. You can go to the GIVENNAME, or MIDDLENAME or SURNAME paths, no matter how many GIVENNAME or MIDDLENAME, or in this example, SURNAME you have, it would be ok.

For example:

```auto
{
	"GIVENNAME": [
		"May",
		"Mary"
	],
	"MIDDLENAME": [
		"Green",
		"Herbs"
	],
	"SURNAME": [
		"Dowman",
		"Hinna"
	]
}

```

That would not be a problem using JSON, you still have 3 paths.

But if I translate the above example to the current given format, it would look like:

```auto
[ {
  "string" : "May",
  "termType" : "GIVENNAME"
}, {
  "string" : "Mary",
  "termType" : "GIVENNAME"
}, {
  "string" : "Green",
  "termType" : "MIDDLENAME"
}, {
  "string" : "Herbs",
  "termType" : "MIDDLENAME"
}, {
  "string" : "Dowman",
  "termType" : "SURNAME"
}, {
  "string" : "Hinna",
  "termType" : "SURNAME"
} ]

```

Now, suddenly I got 6 pairs of paths vs 4 pairs from the original data.

If I have to access each path, I have to access them by position/index, for example `<some_level>.string[2]` or `<some_level>.termType[2]`, and we can also speculate that the amount of pairs will be dynamic per record, so you would need to determine what’s the `n` position/index will be (`<some_level>.string[n]`).

Obviously it can be done, but it looks more work to implement, such as figuring out the size of n, looping to retrieve the string[0] and termType[0] to string[n] and termType[n] (I am guessing that’s how it got implemented in the Java snippet), etc.

I would still be interested to see how you implement it with JSON Path though.

---

<div class="post-metadata">

**Author:** ![daniela\_digles](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/b9bd4f/32.png) [@daniela\_digles](https://forum.knime.com/u/daniela_digles)\
**Post date:** [July 27, 2021, 8:13pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/8 "2021-07-27T20:13:16Z")

</div>

Hi @bruno29a, you could use the following JSON Path queries (all as lists):  
`$..[?(@.termType=="GIVENNAME")].string`  
`$..[?(@.termType=="MIDDLENAME")].string`  
`$..[?(@.termType=="SURNAME")].string`

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [July 27, 2021, 9:35pm UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/9 "2021-07-27T21:35:15Z")

</div>

Very nice one @daniela_digles !!! I didn’t know we could do this, it’s a good way to search by values.

I used your JSON Path and it can be done in a few nodes (In the v2 one, where I added both samples):

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/3/2/3246e482753189c9a39bc69ab58812563c86f755.png)

JSON Path config:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/9/3/9336444adeddded30924593943e74d64e924693e.png)

JSON Path results:  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/1/c/1c6dd294c08f0738a429210de3d4dace187806c8.png)

Final results:  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/a/1/a1fdcd86a3140a4aabc7078be4683f053e36b805.png)

FYI: v1 can process both types of sample, but because of the transpose and moving of first row as header, it cannot process both at the same time.

Here’s the workflow:  
[Process multiple occurance of same field in JSON with different values v2.knwf](https://forum.knime.com/uploads/short-url/oPulqbagkxSWAd1T36zcSBSxPRX.knwf) (30.5 KB)

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [January 26, 2022, 9:36am UTC](https://forum.knime.com/t/process-multiple-occurance-of-same-field-in-json-with-different-values/34926/10 "2022-01-26T09:36:05Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
